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. POJ2031Building a Space Station

    http://poj.org/problem?id=2031 题意:你是空间站的一员,太空里有很多球形且体积不一的“小房间”,房间可能相距不近,也可能是接触或者甚至是重叠关系,所有的房间都必须相连,这 ...

  2. select下拉框选择触发事件

    我一直以来都认为,select 下拉框选择对选项 options 使用 onclick 注册事件即可,如下: <select> <option value="0" ...

  3. TPS和QPS的区别

    一.TPS:Transactions Per Second(每秒传输的事物处理个数),即服务器每秒处理的事务数.TPS包括一条消息入和一条消息出,加上一次用户数据库访问.(业务TPS = CAPS × ...

  4. Hibernate逍遥游记-第2章-使用hibernate.properties

    1. package mypack; import org.hibernate.*; import org.hibernate.cfg.Configuration; import java.util. ...

  5. R语言学习笔记:列表

    R列表时以其他对象为成分的有序集合,列表的成分和向量不同,它们不一定是同一种数据类型,模式或者长度.例: > my.list<-list(stud.id=34453, + stud.nam ...

  6. VMWare-NAT模式实现局域网其他主机对虚拟机访问

    WIN 2012在桥接模式下可以实现主机及主机所在局域网内其他主机对虚拟机的访问,但是在NAT模式下主机可以对虚拟机访问,但是主机所在的局域网内其他主机却无法对虚拟机访问,必须进行主机转发,从而实现局 ...

  7. E-BOM和M-BOM的区别

    简单一点,ENG BOM一般用于试产,正式BOM一般用于量产:ENG BOM是FOR RD设计用的.即TEMP档.并非正式区的.一般的电子零件类的企业都会用到ENG BOM.在EBS中,ENG BOM ...

  8. 1628. White Streaks(STL)

    1628 题意不太好理解 求横黑条 和竖黑条共有多少个 注意1*1的情况 如果横向纵向都是1*1 算为一个 否则不算 用了下vector  枚举找下 #include <iostream> ...

  9. poj3686The Windy's (KM)

    http://poj.org/problem?id=3686 拆点很巧妙 将每个M个点拆成m*n个点 分别表示第i个玩具在第j个机器上倒数第K个处理 假设这k个玩具真正用在加工的时间分为a1,a2,a ...

  10. [swustoj 917] K-lucky-number

    K-lucky-number(0917) 问题描述 K-lucky-number is defined as add up the number of each bit is a multiple o ...