(一)备注:方法也可以向变量一样传值例如
package main import "fmt" type myfunc func (int,int) int; func goinfo (n1,n2 int) int { return n1+n2; } func handle (handleFunc myfunc,n1,n2 int) int { return handleFunc(n1,n2) } func main() { aa:=goinfo; sum:=handle(aa,20,10); fmt.Println(sum); }
(二)go里面算法书写
package main import ( "fmt" "math/rand" "time" ) //倒叙 func resver( arr []int){ var rever_arr []int; for i:=len(arr)-1;i>=0;i-- { rever_arr = append(rever_arr,arr[i]); } fmt.Println(rever_arr); } //排序冒泡排序 func buddleSort(arr []int) { for i:=0;i<len(arr)-1;i++ { for j:=0;j<len(arr)-1-i;j++ { if arr[j] > arr[j+1] { tmp:=0; tmp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = tmp; } } } fmt.Println(arr); } func main() { var nums []int; rand.Seed(time.Now().Unix()); for i:=0;i<10;i++ { nums=append(nums,rand.Intn(100)); } fmt.Println(nums); //resver(nums); //buddleSort(nums); }