package main import( "fmt" "strings" "project04/utils" //這個引入包,從環境變量GOPATH或者GOROOT起須要一個src目錄; ) type myfunc func(int, int) int //自定義類型myfunc //全局匿名函數 var ( FuncGlobalAnonymous = func(n1 int, n2 int) int { return n1 - n2 } ) func main() { //變量的定義以及switch語句的使用 var num1 float64 = utils.Test(1, 2, '+') var num2 float64 = utils.Test(1, 2, '-') var num3 float64 = utils.Test(1, 2, '*') var num4 float64 = utils.Test(1, 3, '/') var num5 float64 = utils.Test(1, 1, '&') fmt.Println("num1 = ", num1) fmt.Println("num2 = ", num2) fmt.Println("num3 = ", num3) fmt.Printf("num4 = %.2f\r\n", num4) fmt.Println("num5 = ", num5) //函數的使用 sub,sum := getSubAndSum(1, 2) fmt.Printf("sub = %v\r\n", sub) fmt.Printf("sum = %v\r\n", sum) //返回值有兩個,第一個值忽略 _,sum = getSubAndSum(2, 2) fmt.Printf("sum = %v\r\n", sum) //測試遞歸 testRecursion(4);//2,2,3 //for語句的使用 i := 1這種寫法自動判斷類型; for i := 1; i <= 3; i++ { fmt.Printf("test for i = %d\r\n", i) } //指針類型 var n1 int = 10; testZhizhen(&n1);//傳入n1地址 fmt.Printf("testzz n1 = %d\r\n", n1)//n1 爲20了; //函數賦值給變量 fval := funcValue; fmt.Printf("fval 類型是%T,funcValue類型是%T\r\n", fval, funcValue) res := fval(1, 3) fmt.Println("res=", res) //函數做爲一個參數傳遞 sub,sum,val := funcParams(fval, 1, 2) fmt.Println("sub=", sub) fmt.Println("sum=", sum) fmt.Println("val=", val) //使用type自定義類型(定義在外圍) mytype := funcCustomType(fval, 2, 3) fmt.Println("mytype=", mytype) //可變參數 total := daynamicParams(10, 1, 2, 3, 4, -1) fmt.Println("total=", total) //init 函數,每一個包都有一個init 函數;能夠用於初始化一些全局變量,在 main函數以前執行 fmt.Println("name=",utils.Name, ",age=", utils.Age) //匿名函數 test := func(n1 int, n2 int) int { return n1 + n2 }(2, 4) fmt.Println("test=", test) test2 := FuncGlobalAnonymous(3, 2) fmt.Println("test2=", test2) test3 := func(n1 int, n2 int) int { return n1 + n2 } test3Val := test3(1, 2) fmt.Println("test3Val=", test3Val) //閉包 tc := testClosure(); tc(1);//11 tc(1);//12 tc(1);//13 f := testClosure2(".jpg") fmt.Println(f("test1")) fmt.Println(f("test2.jpg")) //defer的應用;主要用於關閉資源(數據庫建立連接以後立馬用defer關閉,打開文件以後立馬用defer關閉) tf := testDefer(10, 11) fmt.Println("tf=", tf); //4 } func funcValue(n1 int, n2 int) int { return n1 + n2 } func testDefer(n1 int, n2 int) int { defer fmt.Println("defer1 n1=", n1)//入棧 3 defer fmt.Println("defer2 n2=", n2)//入棧 2 n1++ n2++ fmt.Println("ok!!!!") //這個最早輸出 1 return n1 + n2 } //閉包 func testClosure() func (int) int { var n int = 10 return func (n1 int) int { n = n + n1 fmt.Println("testClosure=", n); return n } } func testClosure2(suff string) func (string) string { return func (name string) string { if !strings.HasSuffix(name, suff) { return name + suff } return name } } ///參數是一個函數, 以及返回值能夠定義 func funcParams(funvar func(int, int) int, n1 int, n2 int) (sub int, sum int, val int) { sub = n1 - n2 sum = n1 + n2 val = funvar(n1, n2) return } //使用自定義類型 func funcCustomType(funvar myfunc, n1 int, n2 int) int { return funvar(n1, n2) } func daynamicParams(n1 int, args... int) int { sum := n1 for i := 0; i < len(args); i++ { sum += args[i] } return sum } //函數的定義 func getSubAndSum(n1 int, n2 int) (int, int) { sub := n1 - n2 sum := n1 + n2 return sub, sum } //至關於php中的引用傳遞 func testZhizhen(n1 *int) { *n1 = *n1 + 10 fmt.Printf("testzz n1 = %d\r\n", *n1) } //遞歸 func testRecursion(n int) { if (n > 2) { n-- testRecursion(n) } fmt.Println("testRecursion n=", n) }
package utils import "fmt" var Name string var Age int func init() { Name = "wangwu" Age = 20 } func Test(n1 float64, n2 float64, operate byte) float64 { var res float64 switch operate { case '+': res = n1 + n2 break; case '-': res = n1 - n2 break case '*': res = n1 * n2 break case '/': res = n1 / n2 break default: fmt.Printf("操做符有誤 %v\r\n", operate) } return res }