$ goctl api new greet
.
├── etc
│ └── greet-api.yaml
├── go.mod
├── greet.api
├── greet.go
└── internal
├── config
│ └── config.go
├── handler
│ ├── greethandler.go
│ └── routes.go
├── logic
│ └── greetlogic.go
├── svc
│ └── servicecontext.go
└── types
└── types.go
$ cd greet
$ go run greet.go -f etc/greet-api.yaml
将看到如下报错
$ go run greet.go etc/greet-api.yaml
greet.go:11:2: cannot find package "github.com/tal-tech/go-zero/core/conf" in any of:
/usr/local/go/src/github.com/tal-tech/go-zero/core/conf (from $GOROOT)
/home/username/Develop/Go/src/github.com/tal-tech/go-zero/core/conf (from $GOPATH)
internal/logic/greetlogic.go:9:2: cannot find package "github.com/tal-tech/go-zero/core/logx" in any of:
/usr/local/go/src/github.com/tal-tech/go-zero/core/logx (from $GOROOT)
/home/username/Develop/Go/src/github.com/tal-tech/go-zero/core/logx (from $GOPATH)
greet.go:12:2: cannot find package "github.com/tal-tech/go-zero/rest" in any of:
/usr/local/go/src/github.com/tal-tech/go-zero/rest (from $GOROOT)
/home/username/Develop/Go/src/github.com/tal-tech/go-zero/rest (from $GOPATH)
internal/handler/greethandler.go:10:2: cannot find package "github.com/tal-tech/go-zero/rest/httpx" in any of:
/usr/local/go/src/github.com/tal-tech/go-zero/rest/httpx (from $GOROOT)
/home/username/Develop/Go/src/github.com/tal-tech/go-zero/rest/httpx (from $GOPATH)
使用 Go Modules 初始化项目并再次运行,Go Modules 将自动下载所需依赖,并在依赖下载完成后启动服务:
$ go mod init
go: creating new go.mod: module greet
$ go run greet.go etc/greet-api.yaml
go: finding module for package github.com/tal-tech/go-zero/core/conf
go: finding module for package github.com/tal-tech/go-zero/rest
go: finding module for package github.com/tal-tech/go-zero/rest/httpx
go: finding module for package github.com/tal-tech/go-zero/core/logx
go: found github.com/tal-tech/go-zero/core/conf in github.com/tal-tech/go-zero v1.0.22
go: found github.com/tal-tech/go-zero/rest in github.com/tal-tech/go-zero v1.0.22
go: found github.com/tal-tech/go-zero/rest/httpx in github.com/tal-tech/go-zero v1.0.22
go: found github.com/tal-tech/go-zero/core/logx in github.com/tal-tech/go-zero v1.0.22
Starting server at 0.0.0.0:8888...
输出结果:
Starting server at 0.0.0.0:8888...
$ curl -i http://localhost:8888/from/you
输出结果
HTTP/1.1 200 OK
Content-Type: application/json
Date: Thu, 22 Oct 2020 11:31:46 GMT
Content-Length: 14
{"message":""}
服务端输出:
Starting server at 0.0.0.0:8888...
{"@timestamp":"2020-10-22T11:31:46.248+08","level":"info","content":"200 - /from/you - 127.0.0.1:56136 - curl/7.68.0 - 0.1ms","trace":"6b2d6d3c733233e9","span":"0"}
环境:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.1 LTS
Release: 20.04
Codename: focal
$ go version
go version go1.15.2 linux/amd64
$ GO111MODULE=on GOPROXY=https://goproxy.cn/,direct go get -u github.com/tal-tech/go-zero/tools/goctl
go: found github.com/tal-tech/go-zero/tools/goctl in github.com/tal-tech/go-zero v1.0.22
go: downloading github.com/urfave/cli v1.22.4
go: downloading github.com/logrusorgru/aurora v2.0.3+incompatible
go: downloading github.com/iancoleman/strcase v0.0.0-20191112232945-16388991a334
go: downloading github.com/go-sql-driver/mysql v1.5.0
go: downloading github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d
go: downloading github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2
go: downloading github.com/spaolacci/murmur3 v1.1.0
go: downloading github.com/russross/blackfriday/v2 v2.0.1
go: downloading go.uber.org/automaxprocs v1.3.0
go: downloading github.com/shurcooL/sanitized_anchor_name v1.0.0
go: downloading github.com/emicklei/proto v1.9.0
go: downloading gopkg.in/yaml.v2 v2.2.8
go: github.com/go-sql-driver/mysql upgrade => v1.5.0
go: gopkg.in/yaml.v2 upgrade => v2.3.0
go: github.com/urfave/cli upgrade => v1.22.4
go: github.com/cpuguy83/go-md2man/v2 upgrade => v2.0.0
go: github.com/iancoleman/strcase upgrade => v0.1.2
go: downloading github.com/cpuguy83/go-md2man/v2 v2.0.0
go: downloading github.com/iancoleman/strcase v0.1.2
go: downloading gopkg.in/yaml.v2 v2.3.0
$ export GOPATH=$(go env GOPATH)
$ export PATH="$GOPATH/bin:$PATH"
$ goctl -v
goctl version 20201021 linux/amd64
第一个终端
$ goctl api new greet
Done.
$ cd greet
$ ls
etc greet.api greet.go internal
$ go run greet.go etc/greet-api.yaml
greet.go:11:2: cannot find package "github.com/tal-tech/go-zero/core/conf" in any of:
/usr/local/go/src/github.com/tal-tech/go-zero/core/conf (from $GOROOT)
/home/username/Develop/Go/src/github.com/tal-tech/go-zero/core/conf (from $GOPATH)
internal/logic/greetlogic.go:9:2: cannot find package "github.com/tal-tech/go-zero/core/logx" in any of:
/usr/local/go/src/github.com/tal-tech/go-zero/core/logx (from $GOROOT)
/home/username/Develop/Go/src/github.com/tal-tech/go-zero/core/logx (from $GOPATH)
greet.go:12:2: cannot find package "github.com/tal-tech/go-zero/rest" in any of:
/usr/local/go/src/github.com/tal-tech/go-zero/rest (from $GOROOT)
/home/username/Develop/Go/src/github.com/tal-tech/go-zero/rest (from $GOPATH)
internal/handler/greethandler.go:10:2: cannot find package "github.com/tal-tech/go-zero/rest/httpx" in any of:
/usr/local/go/src/github.com/tal-tech/go-zero/rest/httpx (from $GOROOT)
/home/username/Develop/Go/src/github.com/tal-tech/go-zero/rest/httpx (from $GOPATH)
$ go mod init
go: creating new go.mod: module greet
$ go run greet.go etc/greet-api.yaml
go: finding module for package github.com/tal-tech/go-zero/core/conf
go: finding module for package github.com/tal-tech/go-zero/rest
go: finding module for package github.com/tal-tech/go-zero/rest/httpx
go: finding module for package github.com/tal-tech/go-zero/core/logx
go: found github.com/tal-tech/go-zero/core/conf in github.com/tal-tech/go-zero v1.0.22
go: found github.com/tal-tech/go-zero/rest in github.com/tal-tech/go-zero v1.0.22
go: found github.com/tal-tech/go-zero/rest/httpx in github.com/tal-tech/go-zero v1.0.22
go: found github.com/tal-tech/go-zero/core/logx in github.com/tal-tech/go-zero v1.0.22
Starting server at 0.0.0.0:8888...
{"@timestamp":"2020-10-22T23:22:17.248+08","level":"info","content":"200 - /from/you - 127.0.0.1:56136 - curl/7.68.0 - 0.1ms","trace":"6b2d6d3c733233e9","span":"0"}
当服务起来后启动另一个终端
$ curl -i http://localhost:8888/from/you
HTTP/1.1 200 OK
Content-Type: application/json
Date: Thu, 22 Oct 2020 15:26:00 GMT
Content-Length: 14
{"message":""}