go创建模块(第三部分)

2023-07-07  本文已影响0人  护念

6. 添加测试

  1. 在greetings目录下,新建一个测试文件touch greetings_test.go
// greetings_test.go
package greetings

import (
    "testing" // go原生支持
    "regexp" // 原生支持
)

// 测试输入一个name后能正常返回
func TestHelloName(t *testing.T) {
    name := "Gladys"

    // 正则其中包含名称 name
    want := regexp.MustCompile(`\b`+name+`\b`)
    msg, err := Hello("Gladys")
    if !want.MatchString(msg) || err != nil {
        t.Fatalf(`Hello("Gladys") = %q, %v, want match for %#q, nil`, msg, err, want)
    }
}

// 测试名称为空时 应返回错误
func TestHelloEmpty(t *testing.T) {
    msg, err := Hello("")
    if msg != "" || err == nil {
        t.Fatalf(`Hello("") = %q, %v, want "", error`, msg, err)
    }
}
  1. 在greetings目录下运行go test
 dongmingyan@pro ⮀ ~/go_playground/greetings ⮀ go test
PASS
ok      example.com/greetings   0.547s
 dongmingyan@pro ⮀ ~/go_playground/greetings ⮀ go test -v
=== RUN   TestHelloName
--- PASS: TestHelloName (0.00s)
=== RUN   TestHelloEmpty
--- PASS: TestHelloEmpty (0.00s)
PASS
ok      example.com/greetings   0.118s

小结:

  1. go原生支持测试(testing)
  2. go中的测试通过函数实现,函数名表明测试项
  3. 发现测试不通过直接产生错误
  4. 命令go testgo test -v

7.编译和安装应用

前面我们已经写了些go代码,但是对go命令的了解还少,这里学习下go相关的命令

  1. 我们来到hello目录下,执行go build,此时生成了hello 可执行文件,在当前目录下 ./hello 可执行
dongmingyan@pro ⮀ ~/go_playground/hello ⮀ go build
 dongmingyan@pro ⮀ ~/go_playground/hello ⮀ tree
.
├── go.mod
├── hello
└── hello.go

1 directory, 3 files
 dongmingyan@pro ⮀ ~/go_playground/hello ⮀ ./hello
map[lisi:Hail, lisi! Well met! wangwu:Great to see you, wangwu! 张三:Hail, 张三! Well met!]
  1. go install的作用是生成可执行文件并放置到配置的bin目录下
    首先我们看下当前执行会放到的目录是哪个,执行命令go list -f '{{.Target}}'
dongmingyan@pro ⮀ ~/go_playground/hello ⮀ go list -f '{{.Target}}'
/Users/dongmingyan/go/bin/hello

可以看到放置的路径是/Users/dongmingyan/go/bin,这里本质上是默认的GOPATH下的bin路径
我们可以设置GOBIN路径来更改此路径,执行go env -w GOBIN=/usr/local/go/bin

dongmingyan@pro ⮀ ~/go_playground/hello ⮀ go env | grep GOPATH
GOPATH="/Users/dongmingyan/go"
 dongmingyan@pro ⮀ ~/go_playground/hello ⮀ go env -w GOBIN=/usr/local/go/bin
 dongmingyan@pro ⮀ ~/go_playground/hello ⮀ go list -f '{{.Target}}'
/usr/local/go/bin/hello

将这个bin路径放到PATH变量中,那么执行go install后在任意目录下,都可以执行我们生成的命令

  1. 执行go install
    上一步我们已经将路径改成了/usr/local/go/bin,在hello目录下执行go install,然后去/usr/local/go/bin检查可以看到我们生成的hello可执行文件
dongmingyan@pro ⮀ ~/go_playground/hello ⮀ sudo go install
Password:
 dongmingyan@pro ⮀ ~/go_playground/hello ⮀ cd /usr/local/go/bin
 dongmingyan@pro ⮀ /usr/local/go/bin ⮀ ls
go    gofmt hello

补充go环境变量go env

dongmingyan@pro ⮀ /usr/local/go/bin ⮀ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN="/usr/local/go/bin"
GOCACHE="/Users/dongmingyan/Library/Caches/go-build"
GOENV="/Users/dongmingyan/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/dongmingyan/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/dongmingyan/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.20.5"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/j8/08btplt912l0m3jg760nkbpw0000gn/T/go-build496445168=/tmp/go-build -gno-record-gcc-switches -fno-common"

当前关注以下几个

小结:

上一篇 下一篇

猜你喜欢

热点阅读