学习go语言 我的习题答案 chapter3
package main import ( "fmt" "strings" ) func main() { /* for i := 1; i<=100; i++ { fmt.Println(strings.Repeat("A", i)) } for i := 1; i<=100; i++ { switch { case i%3==0 && i%5==0 : fmt.Println("FizzBuzz") case i%5==0: fmt.Println("Buzz") case i%3==0: fmt.Println("Fizz") default: fmt.Println(i) } } for i := 0; i<10; i++ { fmt.Println(i) } var i int LOOP: if i < 10 { fmt.Println(i) i += 1 goto LOOP } var arr [10]int for i := range arr { fmt.Println(i) } */ } pengphy@Pengfei-Xue dsproxy_go$ cat chapter3.go package main import ( "fmt" ) func getOrderedArg(a,b int) (small, big int) { small = a big = b if a > b { small = b big = a } return } func float64Mean(data_in []float64) (mean float64) { data_in_len := len(data_in) if data_in_len == 0 { return 0.0 } sum := 0.0 for _, value := range data_in { sum += value } mean = sum / float64(data_in_len) return } type Stack []int func (s Stack) push(value int) Stack { if len(s)+1 > cap(s) { fmt.Println("stack is full, can not store more") } s = append(s, value) return s } func (s Stack) pop() (st Stack, value int) { if len(s) == 0 { fmt.Println("Stack is empty, can not pop") } value = s[len(s)-1] st = s[:(len(s)-1)] return } func main() { /* s := make(Stack, 0, 20) fmt.Println(s) s = s.push(32) fmt.Println(s) s, value := s.pop() fmt.Println(value) fmt.Println(s) fmt.Println(getOrderedArg(1,2)) fmt.Println(getOrderedArg(4,2)) test_float64_slice := [...]float64{0.0,1.0,2.3232,4e-5,43.00} fmt.Println(float64Mean(test_float64_slice[:])) for i:= 0; i<10; i++ { defer fmt.Println(i) } */ }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。