GO 入門


版本管理工具 g

go stable版本

g ls-remote stable

切換版本

g use 

已安裝版本

g ls

單元測試(Unit Test)

程式範例(加法)

資料結構

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

代碼

calculate/add.go

package calculate

func Add(a, b int) int {
	return a + b
}

calculate/add_test.go

package calculate

import "testing"

func TestAdd(t *testing.T) {
	if Add(1, 2) != 3 {
		t.Errorf("1+2=%d wrong number", Add(1, 2))
	}
}

main.go

package main

import (
	"0216/calculate"
	"fmt"
)

func main() {

	fmt.Println(calculate.Add(1, 2))
}

測試指令

功能測試

go test -v ./calculate

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

覆蓋測試

go test -v ./calculate -cover

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

go test -coverprofile=coverage.out .

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

go tool cover -html=coverage.out

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

效能測試

go test -v ./calculate -bench="."

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →