1.安装框架,创建第一个web api
package main import ( "github.com/micro/go-micro/web" "net/http" ) func main() { /*第一种方法,新建一个Option类型的函数,作为参数传递给NewService方法 addr := func(o *Options){ o.Address = "8001" } server := web.NewService(addr) */ /*第二种方法,使用官方提供的简易方法web.Address(":8001") func Address(a string) Option { return func(o *Options) { o.Address = a } } //其实是对第一种方法的简易封装 */ server := web.NewService(web.Address(":8001")) //参数为不定长参数Option type Option func(o *Options) server.HandleFunc("http://www.mamicode.com/", func(writer http.ResponseWriter, request *http.Request) { writer.Write([]byte("hello world")) }) server.Run() }
,温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/44876.html