This is a creation in Article, where the information may have evolved or changed.
The function application of Golang
I used to learn Java, so it seems there are a lot of problems with the go language:
- Never set a variable that is useless at all
- Do not write semicolons
First of all, learn about the variable definition of the go language, etc.
I'll copy the code.
package mainimport ("fmt")type Books struct{ title string author string subject string}func main() { //分配内存,不够内存为空 Books1 :=new(Books) Books1.title="go语言开发" Books1.author="多啦A梦" Books1.subject="programme" printBooks(Books1)}//分配内存需要func printBooks(book1 *Books) { fmt.Println(book1.title) fmt.Println(book1.author) fmt.Println(book1.subject)}
Used to be Java for the pointer is still very strange, so study the memory address management should be *books.
Creating a function is Books is creating and then adding attributes to the back.
That means fun xxx () int/char/float{} and then it's built.
Like a simple computational question.
PackageMainImport("FMT")funcMain () {FMT. Println (Jiahao( as, the))//This very good can return two values //If it is Java can only return the array and don't believe everyone tryFmt. Println (jiahao1( as, the)) }funcJiahao (Jint, Iint)int{returnI+J} fun Jiahao1 (jint, Iint)(int,int){returnJ+i,j-i}}
And then this simple thing is done switch
PackageMainImport("FMT")funcMain () {//Simple use of switch means no parenthesesTest" the")}funcTest (tstring) {SwitchR = Case " the": FMT. Print ("Queen") Case "Shao": FMT. Print ("KING") Case " the": FMT. Print ("ING") Case " the": FMT. Print ("Guang") }}
And then there's the For loop.
PackageMainImport "FMT"funcMain () {varBint= the varAint //Top is to declare an array xx:= description is not to declare what XX is and then assign to itShu: =[6]int{1,2,3,4,5,6}//loop with a semicolon forA: =0; A <Ten; a++ {fmt. Printf ("Value of a:%d\n", a)}//Loop out arrays forI=0; I<6; i++ {fmt. Println (" ", Shu[i])}//This is a Foreach Loop traversal array fori,x:=RangeShu {println("Positive is"I"Values"Is,shu[i])}}
It solves the basic problem, and then the most important thing comes.
Because the go language array cannot be changed, create an array that can be automatically added, so there is a (Slice) slice of this thing.
PackageMainImport("FMT")funcMain () {//create slices, defined in many ways make ([]type,len)Slice1: = Make([]int,0)//or set a size like thisSlice1: = S[0:]//And thiss: = []int{1,2,3,4,5,6,3,232, $,432,2}//append uses append when the space is full to increase by one times. So you don't have to consider the widthSlice1 =Append(Slice1, -, -,304, -,2)//Then traverse it out fori:=Ranges{FMT. Println (" Capital of", S[i])}}