go中不同模块的互相调用、go test用于对go程序进行测试

参考:Tutorial: Create a Go module

1.创建一个module

mkdir greetings
cd greetings
go mod init example.com/greetings

greetings.go

package greetings

import "fmt"

// Hello returns a greeting for the named person.
func Hello(name string) string { // 形参为string,返回值为string
    // Return a greeting that embeds the name in a message.
    message := fmt.Sprintf("Hi, %v. Welcome!", name) 
    return message 
}
  • 在Go中,一个名字以大写字母开头的函数可以被不在同一个包中的函数调用。
  • :=运算符是一种在一行中声明和初始化变量的快捷方式(Go 使用右侧的值来确定变量的类型),下面是一个种更长远考虑的写法:
var message string
message = fmt.Sprintf("Hi, %v. Welcome!", name)

2. 从另一个module调用

cd ..
mkdir hello
cd hello
go mod init example.com/hello

hello.go

package main

import (
    "fmt"

    "example.com/greetings"
)

func main() {
    // Get a greeting message and print it.
    message := greetings.Hello("Gladys")
    fmt.Println(message)
}
go mod edit -replace example.com/greetings=../greetings

该命令指定 example.com/greetings 应替换为 ../greetings 以定位依赖项。
go.mod文件变成了如下:

module example.com/hello

go 1.16

replace example.com/greetings => ../greetings
go mod tidy

运行go mod tidy命令以同步 example.com/hello 模块的依赖项,添加代码所需但尚未在模块中跟踪的依赖项。
go.mod文件变成了如下:

module example.com/hello

go 1.16

replace example.com/greetings => ../greetings

require example.com/greetings v0.0.0-00010101000000-000000000000

运行:

go run .
输出:
Hi, Gladys. Welcome!

3.返回和处理错误

greetings.go修改为:

package greetings

import (
    "errors"
    "fmt"
)

// Hello returns a greeting for the named person.
func Hello(name string) (string, error) {
    // If no name was given, return an error with a message.
    if name == "" {
        return "", errors.New("empty name")
    }

    // If a name was received, return a value that embeds the name
    // in a greeting message.
    message := fmt.Sprintf("Hi, %v. Welcome!", name)
    return message, nil
}

调用者将检查第二个值以查看是否发生错误。 nil代表没有错误

hello.go修改为:

package main

import (
    "fmt"
    "log"

    "example.com/greetings"
)

func main() {
    // Set properties of the predefined Logger, including
    // the log entry prefix and a flag to disable printing
    // the time, source file, and line number.
    log.SetPrefix("greetings: ")
    log.SetFlags(0)

    // Request a greeting message.
    message, err := greetings.Hello("")
    // If an error was returned, print it to the console and
    // exit the program.
    if err != nil {
        log.Fatal(err)
    }

    // If no error was returned, print the returned message
    // to the console.
    fmt.Println(message)
}

运行:

go run .
输出:
greetings: empty name
exit status 1

4.返回随机问候语——math/rand的使用

greetings.go修改为:

package greetings

import (
    "errors"
    "fmt"
    "math/rand"
    "time"
)

// Hello returns a greeting for the named person.
func Hello(name string) (string, error) {
    // If no name was given, return an error with a message.
    if name == "" {
        return name, errors.New("empty name")
    }
    // Create a message using a random format.
    message := fmt.Sprintf(randomFormat(), name)
    return message, nil
}

// init sets initial values for variables used in the function.
func init() { // 在全局变量初始化之后,Go 在程序启动时自动执行 init 函数
    rand.Seed(time.Now().UnixNano())
}

// randomFormat returns one of a set of greeting messages. The returned
// message is selected at random.
func randomFormat() string {
    // A slice of message formats.
    formats := []string{ // 在括号中省略其大小,这告诉 Go 切片下的数组大小可以动态更改。
        "Hi, %v. Welcome!",
        "Great to see you, %v!",
        "Hail, %v! Well met!",
    }

    // Return a randomly selected message format by specifying
    // a random index for the slice of formats.
    return formats[rand.Intn(len(formats))]
}

hello.go修改为:

package main

import (
    "fmt"
    "log"

    "example.com/greetings"
)

func main() {
    // Set properties of the predefined Logger, including
    // the log entry prefix and a flag to disable printing
    // the time, source file, and line number.
    log.SetPrefix("greetings: ")
    log.SetFlags(0)

    // Request a greeting message.
    message, err := greetings.Hello("Gladys")
    // If an error was returned, print it to the console and
    // exit the program.
    if err != nil {
        log.Fatal(err)
    }

    // If no error was returned, print the returned message
    // to the console.
    fmt.Println(message)
}

运行:

$ go run .
Great to see you, Gladys!

$ go run .
Hi, Gladys. Welcome!

$ go run .
Hail, Gladys! Well met!

5.形参为数组、返回值为map

greetings.go修改为:

package greetings

import (
    "errors"
    "fmt"
    "math/rand"
    "time"
)

// Hello returns a greeting for the named person.
func Hello(name string) (string, error) {
    // If no name was given, return an error with a message.
    if name == "" {
        return name, errors.New("empty name")
    }
    // Create a message using a random format.
    message := fmt.Sprintf(randomFormat(), name)
    return message, nil
}

// Hellos returns a map that associates each of the named people
// with a greeting message.
func Hellos(names []string) (map[string]string, error) {
    // A map to associate names with messages.
    messages := make(map[string]string)
    // Loop through the received slice of names, calling
    // the Hello function to get a message for each name.
    for _, name := range names { // range返回循环中当前项目的索引和项目值的副本。
        message, err := Hello(name)
        if err != nil {
            return nil, err
        }
        // In the map, associate the retrieved message with
        // the name.
        messages[name] = message
    }
    return messages, nil
}

// Init sets initial values for variables used in the function.
func init() {
    rand.Seed(time.Now().UnixNano())
}

// randomFormat returns one of a set of greeting messages. The returned
// message is selected at random.
func randomFormat() string {
    // A slice of message formats.
    formats := []string{
        "Hi, %v. Welcome!",
        "Great to see you, %v!",
        "Hail, %v! Well met!",
    }

    // Return one of the message formats selected at random.
    return formats[rand.Intn(len(formats))]
}

hello.go修改为:

package main

import (
    "fmt"
    "log"

    "example.com/greetings"
)

func main() {
    // Set properties of the predefined Logger, including
    // the log entry prefix and a flag to disable printing
    // the time, source file, and line number.
    log.SetPrefix("greetings: ")
    log.SetFlags(0)

    // A slice of names.
    names := []string{"Gladys", "Samantha", "Darrin"}

    // Request greeting messages for the names.
    messages, err := greetings.Hellos(names)
    if err != nil {
        log.Fatal(err)
    }
    // If no error was returned, print the returned map of
    // messages to the console.
    fmt.Println(messages)
}

6.对函数进行测试——go test

在 greetings 目录中,创建一个名为 greetings_test.go 的文件(以 _test.go 结尾的文件名告诉 go test 命令该文件包含测试函数。),内容如下:

package greetings

import (
    "testing"
    "regexp"
)

// TestHelloName calls greetings.Hello with a name, checking
// for a valid return value.
func TestHelloName(t *testing.T) { 
    name := "Gladys"
    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)
    }
}

// TestHelloEmpty calls greetings.Hello with an empty string,
// checking for an error. 检测当输入为空时,是否能够返回错误
func TestHelloEmpty(t *testing.T) { 
    msg, err := Hello("")
    if msg != "" || err == nil {
        t.Fatalf(`Hello("") = %q, %v, want "", error`, msg, err)
    }
}

测试函数名称的格式为 TestName,其中 Name 表示特定测试的一些信息。此外,测试函数将指向测试包的 testing.T 类型的指针作为参数,使用此参数的方法来报告和记录您的测试。

测试:

$ go test
PASS
ok      example.com/greetings   0.002s
$ go test -v
=== RUN   TestHelloName
--- PASS: TestHelloName (0.00s)
=== RUN   TestHelloEmpty
--- PASS: TestHelloEmpty (0.00s)
PASS
ok      example.com/greetings   0.002s

greetings.go修改为:

/ Hello returns a greeting for the named person.
func Hello(name string) (string, error) {
    // If no name was given, return an error with a message.
    if name == "" {
        return name, errors.New("empty name")
    }
    // Create a message using a random format.
    // message := fmt.Sprintf(randomFormat(), name)
    message := fmt.Sprint(randomFormat())
    return message, nil
}

重新测试:

$ go test
--- FAIL: TestHelloName (0.00s)
    greetings_test.go:15: Hello("Gladys") = "Hi, %v. Welcome!", <nil>, want match for `\bGladys\b`, nil
FAIL
exit status 1
FAIL    example.com/greetings   0.001s

$ go test -v
=== RUN   TestHelloName
    greetings_test.go:15: Hello("Gladys") = "Great to see you, %v!", <nil>, want match for `\bGladys\b`, nil
--- FAIL: TestHelloName (0.00s)
=== RUN   TestHelloEmpty
--- PASS: TestHelloEmpty (0.00s)
FAIL
exit status 1
FAIL    example.com/greetings   0.001s

在没有 -v 标志的情况下运行 go test,输出将仅包含失败测试的结果,这在您有大量测试时很有用。

7.编译和安装程序

从 hello 目录中的命令行运行 go build 命令以将代码编译为可执行文件。

go build // 编译但不安装

您可以通过运行 go list 命令来发现安装路径,如以下示例所示:

go list -f '{{.Target}}'

例如,命令的输出可能是 /home/gopher/bin/hello,这意味着二进制文件已安装到 /home/gopher/bin。您将在下一步中需要此安装目录。

修改go install安装的路径

go env -w GOBIN=/path/to/your/bin

当然这需要你将/path/to/your/bin添加到环境变量PATH中

posted @ 2022-11-24 21:41  好人~  阅读(269)  评论(0编辑  收藏  举报