2:go Basic Knowledge

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
package mainimport  ("FMT") Func main ()  {c ()}// 1, Variable func test1 ()  {//--------- --------------------1.1 Variable declaration var v1 intvar v2 stringvar v3 [10]int //array var  v4 []int   //Array Slicing Var v5 struct {f int}var v6 *int            //Pointer var v7 map[string]int // Key is Mapvar v8 func (a int) with String  value int,  int//----------------------------- 1.2 Initialization of variables var v9 int = 10var v10 = 10v11 := 10//------------------- ----------1.3 Anonymous variable//When a function is called to return multiple values, and you only need to use a certain number of times, then the other variables with "_" instead of _, _, nickname := getname ()}// 1.3  Anonymous variable Supplement Func getname ()   (firstname, lastname, nickname string)  {return   "Michael",  "Ma",  "Liang"}//II: Constant Func test2 ()  {//-----------------------------2.1   Constant Const pi float64 = 3.14159265358979323846const zero = 0.0 //const  (size int64 =  1024eof        = -1 //) const u, v  float32 = 0, 3const a, b, c = 3, 4,  "foo"//----------------- ------------2.2  Reserved Constants//true false iota;iota are special, constants that the compiler can modify, reset to 0 at each const occurrence, and before the next const appears, Each occurrence of a iota will increment 1const  ( // iota is reset to 0c0 = iota // c0 == 0c1 = &NBSP;IOTA&NBSP;//&NBSP;C1&NBSP;==&NBSP;1C2&NBSP;=&NBSP;IOTA&NBSP;//&NBSP;C2&NBSP;==&NBSP;2) const  (A1  = 1 << iota // a == 1  (iota at each const beginning each reset to 0) b1 =  1 << iota // b == 2c3 = 1 << iota // c  == 4) const  (u1          = iota *  42 // u == 0v12 float64 = iota * 42 // v == 42.0w            = iota * 42 // w ==  const x = iota // x == 0  (because Iota is reset to 0 again) const y =  iota //y==0  Ibid.//-----------------------------2.3 Enumerate//Develop a series of related constants as follows const  (sunday =  Iotamondaytuesdaywednesdaythursdayfridaysaturdaynumberofdays)}//Three: Type Func test3 ()  {//---------------- -------------3.1: Base type//Boolean type: bool//integer: int8 byte int16 int uint uintptr//floating-point type: float32  float64//Complex Type: complex64 complex128//string: string//character type: rune//error type: error//---------------------------- -3.2: Composite type//pointer//array//slice//dictionary//channel//struct//interface}//Four: Process Control Func test4 ()  {//----------------------------- 4.1 Select Switch i {case 0:fmt. Printf ("0") case 1:fmt. Printf ("1") case 2:fallthroughcase 3:fmt. Printf ("3") Case 4, 5, 6:fmt. Printf ("4, 5, 6") default:fmt. Printf ("Default")}switch {case 0 <= num && num <= 3:fmt. Printf ("0-3") case 4 <= num && num <= 6:fmt. Printf ("4-6") case 7 <= num && num <= 9:fmt. Printf ("7-9")}//-----------------------------4.2 loop sum := 0for i := 0; i  < 10; i++ {sum += i}sum2 := 0for {sum2++if sum2 >  100 {break}}//-----------------------------4.3 jump//Roll your sister jump IT&NBSP;:=&NBSP;0HERE:FMT. Println (i) it++if i < 10 {goto here}//-----------------------------4.4-piece If a  < 5 {return 0} else {return 1}}//Five: function//----------------------------- 5.1 function definitions//func  functions name   (parameter list) return value {function Body    return statement}//case problem is not Func imafunc (a int, b  int)   (RET&NBSp;int, err error)  {if a < 0 | |  b < 0 {err = errors. New ("should be non-negative numbers!") return}return a + b, nil}//-----------------------------5.2 Function call Func a ()  {// This is how the Imafc, err := imafunc (1, 2) FMT is called. Println (imafc, err)}//-----------------------------5.3 Indeterminate parameter type Func myfunc (args ...int)  {for  _, arg := range args {fmt. Println (ARG)}}//calls Func b ()  {myfunc (1, 2, 3, 4, 5, 6, 7)}//------------ -----------------more than 5.4 return values//See Imafunc function//-----------------------------5.5 anonymous function//instance 1package mainimport  ( "FMT") Func main ()  {func (a, b int)   (sum int)  {sum = a +  Bfmt. PRINTLN (SUM) return} (1,&NBSP;2)//parentheses directly after the argument represents a direct call to the}//instance 2package mainimport  ("FMT") Func main ()   {Nm := func (a, b int)   (suM int)  {sum = a + bfmt. Println (sum) return}nm (1, 2)}//VI: Error handling Func foo (param int) (N int, err error)  { &NBSP, ...} The method is called as follows N, err := foo (0) if err != nil {//  error handling} else {//   Use return value}//seven: defer//-----------------------------7.1func copyfile (dst, src string)   (w  int64, err error)  {srcfile, err := os. Open (SRC)  if err != nil {return}defer srcfile.close () dstfile, err :=  os. Create (DstName) if err != nil {return}defer dstfile.close () Return io. Copy (dstfile, srcfile)}//-----------------------------7.2//panic () and Recover ()//I'm not done yet.


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.