package main func average(values []float64) float64{ if len(values) == 0{ return 0 } sum := 0.0 for _,value := range(values){ sum += value } return sum/float64(len(values)) } func main(){ value := []float64{10,10,12,14,15} println(average(value)) }
package main func swap(a,b int)(int,int){ if(a<b){ return a,b } return b,a } func main(){ a,b := swap(1,2) print(a,b) }
package main import ( "fmt" "strconv" ) type Stack struct{ index int data [10]int } func (this *Stack) Push(i int) int { if(this.index>=10){ return -1 } this.data[this.index] = i this.index++ println(this.index) return 0 } func (this *Stack) Pop() int{ if(this.index<=0){ return -1 } value := this.data[this.index - 1] this.index-- return value } func (this *Stack) String() string{ var str string for i:=0;i<this.index;i++{ str = str + "[" + strconv.Itoa(i) + ":" + strconv.Itoa(this.data[i]) + "]" } return str } func main(){ var obj Stack obj.Push(12) obj.Push(13) obj.Push(14) obj.Pop() print(obj.String()) fmt.Printf("%v",obj) }
package main func testfunc() func(int) int{ return func(a int) int {return a+1} } func main(){ p := testfunc() println(p(2)) }
有疑问加站长微信联系(非本文作者)