返回多人问候语

在您的模块代码中要做的最后一些更改是,您将添加对一次请求获取多人问候语的支持。换句话说,您将处理一个多值输入,然后将该输入中的值与多值输出配对。为此,您需要将一组名称传递给一个可以为其中每个人返回问候语的函数。

但是有一个问题。将 Hello 函数的参数从单个名称更改为一组名称将改变函数的签名。如果您已经发布了 example.com/greetings 模块,并且用户已经编写了调用 Hello 的代码,那么此更改将破坏他们的程序。

在这种情况下,更好的选择是编写一个具有不同名称的新函数。新函数将接受多个参数。这可以保留旧函数以实现向后兼容。

  1. 在 greetings/greetings.go 中,将您的代码更改为如下所示。
    package greetings
    
    import (
        "errors"
        "fmt"
        "math/rand"
    )
    
    // 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 {
            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
    }
    
    // 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))]
    }
    

    在此代码中,您

    • 添加一个 Hellos 函数,其参数是名称切片而不是单个名称。此外,您将其中一个返回类型从 string 更改为 map,以便您可以返回名称与问候消息的映射。
    • 让新的 Hellos 函数调用现有的 Hello 函数。这有助于减少重复,同时也让两个函数都保留下来。
    • 创建一个 messages 地图,将每个收到的名称(作为键)与生成的消息(作为值)关联起来。在 Go 中,您可以使用以下语法初始化地图:make(map[key-type]value-type)。您让 Hellos 函数将此地图返回给调用者。有关地图的更多信息,请参阅 Go 博客上的 Go maps in action
    • 遍历函数收到的名称,检查每个名称是否具有非空值,然后将消息与每个名称关联起来。在此 for 循环中,range 返回两个值:循环中当前项的索引和该项值的副本。您不需要索引,因此您使用 Go 的空标识符(下划线)来忽略它。有关更多信息,请参阅 Effective Go 中的 空标识符
  2. 在您的 hello/hello.go 调用代码中,传递一个名称切片,然后打印您收到的名称/消息地图的内容。

    在 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)
    }
    

    通过这些更改,您将

    • 创建一个 names 变量,作为包含三个名称的切片类型。
    • names 变量作为参数传递给 Hellos 函数。
  3. 在命令行中,切换到包含 hello/hello.go 的目录,然后使用 go run 来确认代码是否正常工作。

    输出应该是一个字符串表示的地图,将名称与消息关联起来,类似如下内容

    $ go run .
    map[Darrin:Hail, Darrin! Well met! Gladys:Hi, Gladys. Welcome! Samantha:Hail, Samantha! Well met!]
    

本主题介绍了用于表示名称/值对的地图。它还介绍了通过为模块中的新功能或更改的功能实现新函数来维护向后兼容性的概念。有关向后兼容性的更多信息,请参阅 保持您的模块兼容

接下来,您将使用内置的 Go 功能为您的代码创建单元测试。