Implement the following types and define ServeHTTP methods on them. Register them to handle specific paths in your web server.

type String string

type Struct struct {
Greeting string
Punct string
Who string
}

For example, you should be able to register handlers using:

http.Handle("/string", String("I'm a frayed knot."))
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
package main

import (
"net/http"
"fmt"
)
type String string type Struct struct {
Greeting string
Punct string
Who string
} func (h Struct) ServeHTTP(
w http.ResponseWriter,
r *http.Request) {
fmt.Fprint(w, h)
}
func (s String) ServeHTTP(
w http.ResponseWriter,
r *http.Request) {
fmt.Fprint(w, s)
} func main() {
http.Handle("/string", String("I'm a frayed knot."))
http.Handle("/struct", &Struct{"Hello", ":", "Gophers!"})
// your http.Handle calls here
http.ListenAndServe("localhost:4000", nil) }

A Tour of Go Exercise: HTTP Handlers的更多相关文章

  1. A Tour of Go Exercise: Images

    Remember the picture generator you wrote earlier? Let's write another one, but this time it will ret ...

  2. A Tour of Go Exercise: Errors

    Copy your Sqrt function from the earlier exercises and modify it to return an error value. Sqrt shou ...

  3. A Tour of Go Exercise: Fibonacci closure

    Let's have some fun with functions. Implement a fibonacci function that returns a function (a closur ...

  4. A Tour of Go Exercise: Maps

    Implement WordCount. It should return a map of the counts of each “word” in the string s. The wc.Tes ...

  5. A Tour of Go Exercise: Slices

    Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit u ...

  6. A Tour of Go Exercise: Loops and Functions

    As a simple way to play with functions and loops, implement the square root function using Newton's ...

  7. exercise.tour.go google的go官方教程答案

    /* Exercise: Loops and Functions #43 */ package main import ( "fmt" "math" ) fun ...

  8. A Tour of Go Advanced Exercise: Complex cube roots

    Let's explore Go's built-in support for complex numbers via the complex64 and complex128 types. For ...

  9. [转]Whirlwind Tour of ARM Assembly

    ref:http://www.coranac.com/tonc/text/asm.htm 23.1. Introduction Very broadly speaking, you can divid ...

随机推荐

  1. ubuntu 和 win7 远程登陆 + vnc登陆

    ubuntu 和 win7 远程登陆: 第一种(通过win7自带的远程桌面来连接ubuntu) 1. windows7配置 我的电脑->属性->远程设置.-----允许远程连接 2. ub ...

  2. gcc: error trying to exec 'cc1plus': execvp: 没有该文件或目录 解决方案

    一般来说,装完linux系统(ubuntu)后,要自己安装java或者c/c++的环境. 这个提示就是说你的系统缺少 g++ 包. 请执行:sudo apt-get install g++  (在ub ...

  3. xcode 树形管理 cocos2d-x的资源

    把资源以目录的形式加入xcode中, 同时, 在加入时, 选择"Create Folder References for any  added folders", 而不是默认的&q ...

  4. MySQL追加注释或者大量修改注释

     MySQL追加注释或者大量修改注释 2016-01-25 20:28:05 分类: MySQL MySQL 5.6.14 之前一个项目比较仓促,开发给的建表语句没有注释.现在要补全注释信息.但是My ...

  5. C# 设置鼠标指针

    鼠标光标指针的使用 #region 设置鼠标指针 //设置鼠标指针 //Cursor cus = new Cursor(@"C:\Users\Public\Pictures\Sample P ...

  6. 食物卡喉别拍背部!救了100多万人性命的“海姆立克急救法"

    先讲三个事例: 一.近日,浙江金华一个17月大的小贝边玩边吃花生,被噎住.10多分钟后,奶奶发现小贝大口喘气,以为他玩累了就抱他回家,等父母赶到送医已晚.小贝大脑受损严重-父母含泪同意放弃治疗,孩子走 ...

  7. python中的commands模块,执行出错:'{' 不是内部或外部命令,也不是可运行的程序 或批处理文件。

    最近发现了python的commands模块,查看了下源码,使用的popen封装的,形成三个函数getstatus(), getoutput(), getstatusoutput() 源码如下: de ...

  8. Spring Data JPA初使用

    我们都知道Spring是一个非常优秀的JavaEE整合框架,它尽可能的减少我们开发的工作量和难度. 在持久层的业务逻辑方面,Spring开源组织又给我们带来了同样优秀的Spring Data JPA. ...

  9. [VC6]ONMESSAGE()宏编译时出现"sytax error ;"错误时

    自定义消息时编译出错,经排查,在定义消息的头文件里 #define WM_XXX (WM_USER+1000); 最后多加了一个分号引起. 吐血.

  10. ☀【document / location】

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...