001_Go hello world

1、go獲取程序參數及指針地址示例函數

package main
import (
	"fmt"
	"os"
)

func main()  {
	fmt.Println(os.Args);

	if len(os.Args) > 1{
		fmt.Println("Hi", os.Args[1])
	}else {
		fmt.Println("Hello world")
		os.Exit(3)
	}

	fmt.Println(*foo())
}


func foo() *string{
	s := "Hello arun"
	return &s
}/*
Hi ggg
Hello arun
*/

(1)go run helloworld.go測試

(2)go build helloworld.go && ./hellowoldui

注意事項:main函數沒有參數和返回值指針

package main
import (
	"fmt"
	"os"
)

//func main() int { //func main must have no arguments and no return values
func main() { //func main must have no arguments and no return values
	fmt.Println("Hello arun!")
	//return 1
	os.Exit(-1)
}

2、編寫測試go測試程序blog

1.源碼文件以_test結尾: xxx_test.goci

2.測試方法名以Test開頭: func TestXXX(t *testing.T) {...}源碼

package fib
import (
	"fmt"
	"testing"
)

func TestFibonacciCreation(t *testing.T)  {
	//t.Log("Log print")
/*	var a int =1
	var b = 1
	var n  int = 5*/

/*	var (
		a int = 1
		b  = 1 //自動類型推斷
		n  int = 5
	)*/

	var a, b , n= 1, 1, 5

	fmt.Print(a)
	for i :=0;/*短變量聲明 := */ i< n; i++{
		fmt.Print(" ", b)
		tmp := a
		a = b
		b = tmp + b
	}
	fmt.Println()
}/*
=== RUN   TestFibonacciCreation
1 1 2 3 5 8
--- PASS: TestFibonacciCreation (0.00s)
PASS
*/

  09:04string

相關文章
相關標籤/搜索