J.J. Huang   2020-11-05   Golang   瀏覽次數:次  

Go - 第八章 | 隨機返回問候

📑 目錄
  1. 簡要
  2. 開始
  3. 過程
  4. 單字/句子
  5. 結語

簡要

在本章節中,你將會更改程式碼,使其不再每次都返回單個問候,而返回幾個預定義的問候消息之一。

開始

  • 為此,你將使用Go切片。一個片就像是一個數組,但你添加和刪除的項目它的動態調整。它是Go中最有用的類型之一。你將添加一小片以包含三個問候消息,然後讓你的程式碼隨機返回其中一個消息。
    • 在greetings / greetings.go中,更改程式碼,如下所示。
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      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() {
      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 a randomly selected message format by specifying
      // a random index for the slice of formats.
      return formats[rand.Intn(len(formats))]
      }
    • 在此程式碼中,你:
      • 添加一個randomFormat函數,該函數返回問候消息的隨機選擇的格式。請注意,randomFormat它以小寫字母開頭,從而使其只能由自己的程序包中的程式碼訪問(換句話說,不會導出)。
      • 在中randomFormat,聲明formats具有三種消息格式的切片。聲明切片時,你可以忽略括號中的大小,如下所示:[]string。這告訴Go,可以動態調整切片下面的數組大小。
      • 使用math/rand程序包生成隨機數,以從切片中選擇項目。
      • 添加一個init函數以rand將當前時間作為包的種子。init初始化全局變量後,Go會在程序啟動時自動執行功能。
      • 在中Hello,調用randomFormat函數以獲取要返回的消息的格式,然後將格式和name值一起使用以創建消息。
      • 像以前一樣返回消息(或錯誤)。
  • 在命令行上,轉到hello目錄,然後運行hello.go確認程式碼可以正常工作。多次運行它,注意問候語已更改。
    • 哦!不要忘了將Gladys的名稱(如果願意,可以添加其他名稱)作為Hellohello.go中函數調用的參數:greetings.Hello("Gladys")
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      $ cd hello
      $ go run ./hello.go
      Great to see you, J.J.!
      $ go run ./hello.go
      Hail, J.J.! Well met!
      $ go run ./hello.go
      Great to see you, J.J.!
      $ go run ./hello.go
      Great to see you, J.J.!
      $ go run ./hello.go
      Hi, J.J.. Welcome!
      $ go run ./hello.go
      Hi, J.J.. Welcome!

過程

Go - 第八章 - 圖 1 (01 min)

單字/句子

單字/句子 翻譯
instead 代替
one of several 幾個之一
predefined 預定義的
slice 片
except 除了
dynamically 動態地
contain 包含
randomly 隨機地
lowercase letter 小寫字母
accessible 無障礙
omit 忽略
brackets 括號
underlying 底層的
math 數學
rand 蘭德
automatically 自動地
needn’t 不需要

結語

這邊教學其重點為「init為函數中使用的變量設置初始值」還有「”math/rand”, “time”」包的使用。​‌‌​​‌‌‌​‌‌​‌‌‌‌​‌‌​‌‌​​​‌‌​​​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​​‌​‌‌​‌​​‌‌​​‌​​​‌‌​​​​​​‌‌​​‌​​​‌‌​​​​​​‌‌​​​‌​​‌‌​​​‌​​‌‌​​​​​​‌‌​‌​‌​​‌​‌‌​‌​‌‌​​‌‌‌​‌‌​‌‌‌‌​‌‌​‌‌​​​‌‌​​​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​​‌​‌‌​‌​​‌‌‌​​​


註:以上參考了
Golang Documentation