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. mysql查看表的结构

    ①mysql > show create table 表名; ②mysql > desc 表名; ③mysql > describe 表名;

  2. 【前端学习】【CSS选择器】

    CSS选择器     CSS选择器 CSS(Cascading Style Sheets)是一项出色的技术,它使得网页的结构和表现样式完全分离.利用CSS选择器能轻松地对某个元素添加样式而不改动HTM ...

  3. QT5.7交叉编译安装到arm(好多系列文章)

    以下采用的系统为ubuntu16.04,开发板为迅为iTOP4412,4.3寸屏. 下载qt5.7源码qt-everywhere-opensource-src-5.7.0.tar.xz http:// ...

  4. java:I/O 根据用户输入反馈信息

    import java.io.*; class userInputIO{ //Java中成员变量有默认初始化,也就是如果不显式设置初始值的话就会被初始化为其类型的默认值(0.false.null等). ...

  5. CodeForces250B——Restoring IPv6(字符串处理)

    Restoring IPv6 DescriptionAn IPv6-address is a 128-bit number. For convenience, this number is recor ...

  6. R语言笔记:快速入门

    1.简单会话 > x<-c(1,2,4) > x [1] 1 2 4 R语言的标准赋值运算符是<-.也可以用=,不过不建议用它,有些情况会失灵.其中c表示连接(concaten ...

  7. Linux配置vpn

    配置如下

  8. 【HDOJ】4056 Draw a Mess

    这题用线段树就MLE.思路是逆向思维,然后每染色一段就利用并查集将该段移除,均摊复杂度为O(n*m). /* 4056 */ #include <iostream> #include &l ...

  9. Poj2948Martian Mining(记忆化)

    链接 这题意好难懂 看得迷迷糊糊 想的也迷迷糊糊  后来睡了会突然想到了..不就是类似以前的矩阵操作 从右下角记忆化   大的由小的推来 dp[i][j] = max(dp[i-1][j]+s1,dp ...

  10. 函数fsp_alloc_seg_inode_page

    分配一个新的inode page /**********************************************************************//** Allocat ...