Learn the go language my exercise answer Chapter3

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed. This chapter is mainly about the use of functions in the go in the process of doing exercises a few of the more memorable problem is the definition of a named return function but did not put it in parentheses the result has been compiled, but I still do not know how to wrong a
And one of the things that impresses me is that go's strong type checking, and the way variables are declared, are clearly the same as the one in C/C + +, before you define an array as if you've seen the pointer in a/C + + definition of where the * is placed and where the array [] should be placed. All data types in Go For example: var test_var int Test_var This variable is of type int this variable declares that if it is in the function of the body of the Var is necessary but can be used inside the function: = to define this and = or the difference between the definition and the assignment is completely separate the more write the more you think it is a C + + upgrade Version hehe
Package Mainimport ("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 < {fmt. PRINTLN (i) i + = 1 goto LOOP} var arr [10]int for I: = Rang E arr {fmt. Println (i)} */}pengphy@pengfei-xue dsproxy_go$ cat Chapter3.go Package Mainimport ("FMT") func getor Deredarg (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 []intfunc (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 m Ain () {/* S: = make (Stack, 0,) fmt. PRINTLN (s) s = s.push (+) fmt.        PRINTLN (s) s, value: = S.pop ()Fmt. Println (value) fmt. Println (s) fmt. Println (Getorderedarg) 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)} */}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.