Skip to content

Instantly share code, notes, and snippets.

@jnriver
Created February 18, 2014 07:46
Show Gist options
  • Save jnriver/9066336 to your computer and use it in GitHub Desktop.
Save jnriver/9066336 to your computer and use it in GitHub Desktop.
Go set http request header
package main
import (
"net/http"
)
func main() {
client := &http.Client{}
reqest, _ := http.NewRequest("GET", "http://localhost:9090", nil)
reqest.Header.Set("Auth-Token", "123")
client.Do(reqest)
}
path: /
method: GET
header: map[User-Agent:[Go 1.1 package http] Auth-Token:[123] Accept-Encoding:[gzip]]
package main
import (
"fmt"
"log"
"net/http"
)
func sayHello(w http.ResponseWriter, r *http.Request) {
fmt.Println("")
fmt.Println("path:", r.URL.Path)
fmt.Println("method:", r.Method)
fmt.Println("header:", r.Header)
fmt.Fprintf(w, "Hello world!") //这个写入到w的是输出到客户端的
}
func main() {
http.HandleFunc("/", sayHello) //设置访问的路由
http.HandleFunc("/login", login) //设置访问的路由
err := http.ListenAndServe(":9090", nil) //设置监听的端口
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment