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

Go - 第七章 | 處理錯誤並返回

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

簡要

處理錯誤是可靠程式碼的必要功能。在本部分中,你將加入一些程式碼以從greetings模塊返回錯誤,然後在呼叫方中進行處理。

開始

  • 在Greetings / greetings.go中,加入下面高亮的程式碼(由於無法標示,所以下面附上圖和程式碼)
    • 如果你不知道該向誰打招呼,則沒有必要回傳問候。如果名稱為空,則將錯誤返回給呼叫方。將以下程式碼複製到greetings.go中並儲存。
      Go - 第七章 - 圖 1 (01)
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      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
      }
    • 在此程式碼中,你:
      • 修改函數,使其返回兩個值:a string和an error。你的呼叫者將檢查第二個值以查看是否發生錯誤。(任何Go函數都可以 返回多個值。)
      • 導入Go標準庫errors軟件包,以便可以使用其errors.New功能。
      • 加入一條if語句以檢查無效請求(名稱應為空字符串),如果請求無效,則返回錯誤。該errors.New函數返回一個error與你的消息中。
      • nil在成功返回中 加入(表示沒有錯誤)作為第二個值。這樣,呼叫者可以看到該函數成功。
  • 在你的hello / hello.go文件中,處理Hello函數現在返回的錯誤以及非錯誤值。
    • 將以下程式碼貼到hello.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
      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)
      }
    • 在此程式碼中,你:
      • 配置log程序包以在其日誌消息的開頭打印命令名稱("greetings:"),而不帶有時間戳或源文件信息。
      • 將兩個Hello返回值(包括)分配error給變量。
      • 將Hello參數從Gladys的名稱更改為空字符串,以便你可以嘗試使用錯誤處理程式碼。
      • 尋找非零error值。在這種情況下繼續下去是沒有意義的。
      • 使用標準庫中的函數log包輸出錯誤信息。如果出現錯誤,請使用log程序包的Fatal功能 來打印錯誤並停止程序。
  • 在hello目錄中的命令行中,運行hello.go確認程式碼可以正常工作。
    • 現在,你傳遞的是一個空名稱,你將得到一個錯誤。
      1
      2
      3
      $ go run hello.go
      greetings: empty name
      exit status 1

過程

Go - 第七章 - 圖 2 (02 min)

單字/句子

單字/句子 翻譯
Handling 處理方式
essential 必要
feature 特徵
solid 固體
highlighted 突出顯示
below 下面
sense 感
occurred 發生
statement 聲明
invalid 無效
successful 成功的
succeeded 成功了
Configure 配置
without 沒有
stamp 郵票
Assign both 同時分配
continuing 持續的
Fatal 致命

結語

這邊的教學,主要是針對錯誤處理和Log的處理,當然還有一個重要的就是「任何Go函數都可以 返回多個值,這與Java是完全不一樣的,所以這要特別注意。​‌‌​​‌‌‌​‌‌​‌‌‌‌​‌‌​‌‌​​​‌‌​​​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​​‌​‌‌​‌​​‌‌​​‌​​​‌‌​​​​​​‌‌​​‌​​​‌‌​​​​​​‌‌​​​‌​​‌‌​​​‌​​‌‌​​​​​​‌‌​‌​​​​‌​‌‌​‌​‌‌​​‌‌‌​‌‌​‌‌‌‌​‌‌​‌‌​​​‌‌​​​​‌​‌‌​‌‌‌​​‌‌​​‌‌‌​​‌​‌‌​‌​​‌‌​‌‌‌


註:以上參考了
Golang Documentation