GF(Go Frame)生产级Go基础开发框架入门

安装:

go get -u -v github.com/gogf/gf

入门:

module TestGoFrame

go 1.13

require (
	github.com/denisenkom/go-mssqldb v0.0.0-20200206145737-bbfc9a55622e // indirect
	github.com/fsnotify/fsnotify v1.4.9 // indirect
	github.com/gf-third/yaml v2.1.0+incompatible // indirect
	github.com/gogf/gf v1.12.1 // indirect
	github.com/gogf/gf-cli v0.0.0-20200323151111-6bccf7ca382b // indirect
	github.com/gogf/gf-swagger v1.0.3 // indirect
	github.com/gorilla/websocket v1.4.2 // indirect
	github.com/grokify/html-strip-tags-go v0.0.0-20200322061010-ea0c1cf2f119 // indirect
	github.com/lib/pq v1.3.0 // indirect
	github.com/mattn/go-runewidth v0.0.9 // indirect
	github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
	golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59 // indirect
	golang.org/x/sys v0.0.0-20200331124033-c3d80250170d // indirect
)
package main
 
import (
    "github.com/gogf/gf/frame/g" //注意新版路径改变
	"github.com/gogf/gf/net/ghttp"
)
 
func Hello1(r *ghttp.Request) {
	var User struct {
		Id   int    `json:"id"`
		Name string `json:"name"`
	}
	User.Id = 1
	User.Name = "dongbao"
	r.Response.WriteJson(User)
}
 
func Hello2(r *ghttp.Request) {
	r.Response.Write("localhost: Hello2!")
}
 
func main() {
	s := g.Server()
    //多域名、多端口测试
	s.Domain("127.0.0.1").BindHandler("/", Hello1)
	s.Domain("localhost").BindHandler("/", Hello2)
	s.SetPort(8080,8081)
	s.Run()
}

编译运行:

访问: http://127.0.0.1:8080/8081/

或者http://localhost:8080/8081/

你可能感兴趣的:(编程经验)