Go Language Learning Notes

Source: Internet
Author: User

//Variable declaration
I: = 10; Declare and assign values directly
J: = 100;
Value of I,j=j,i//transform I,j
Fmt. Printf ("i=%d j=%d\n", i,j)//format lost
Fmt. Println ("i=", I);//cannot use%d etc.

var a int
var b [10]int
var c []int//array slice, equivalent to vector variable array
var s string
var p *int//pointer
var m map[string]int//key is a string type, value is int type
var fun func (a int) int//function
var st struct (//struct
f int
STR string
)

Or you can declare it together.
VAR (
v1 int
S1 string
)

//Anonymous variable
Func GetName () (Firstname,lastname,nickname string) {
Return "may", "Chan", "Chibi Maruko"
}
_,lastname,_: = GetName ()//Just want to take lastName value, others do not want, you can use _ to represent anonymous variables, you can mask off!

//Constant declaration
Const Pi float64 = 3.1415926
Const ZEOR = 0.0
Const (
Size Int64 = 1024
EOF =-1
)
Const AA,BB float64 = 1.0,2.0
Const A,B,C =, "foo"//Declaration and assignment together
Predefined constants: True false Iota
Iota is special, it is reset to 0 when Const appears, and then each time iota occurs before the next const appears, the number represented automatically increases by 1
For example:
Const
Sunday = Iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Numberofdays//This constant is not exported

)


Two methods of string traversal
str: = "Hello, World"
N: = Len (str)
For I: =0;i<n;i++{

CH: = Str[i];
Fmt. Println (I,ch)
}
Fmt. Println ("")

For i,ch: = Range str{//range has two return values, the first return value is an array subscript for the element, and the second return value is the value of the element
Fmt. Println (I,ch)
}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Go Language Learning Notes

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.