一、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 && ./hellowold
注意事项: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) }
二、编写测试go测试程序
1.源码文件以_test结尾: xxx_test.go
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:04