Go Language Basics Summary

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
second//The following code can not be used directly, the go language all declared variables to use, otherwise will be error//The following code is just the Go language basic summary, including from the variable declaration to switch branch package mainimport ("FMT")// DECLARE constant const (i = 0s string = "Test" Start string = "Go language start"//The first uppercase variable is public (to be verified))//Declare a set of global variables Var (k Ints2 St ringflag bool)//enum type iota keyword, default is 0, each autogrow 1const (p1 = Iota//p1=0p2 = Iota//p2=1p3//p3=2p4//p4=3) func testm          AP () {VAR MP map[string]int//declares a MAPMP2: = Make (Map[string]int)//map Another way of declaring mp2["one"] = 1mp2["two"] = 2 Assigns a FMT to map. Println (mp2["both"])//The value corresponding to the key is printed out//map is unordered, each printing is different, can only be obtained by key. Built-in method Len returns the number of keys for the map mp2["one"] = 10//Change the value of one to 10m3: = mp2m3["one"] = 4//At this time mp2["one" of MP2) becomes 4d Elete (MP2, "one")//delete key for one element rating: = map[string]int{"1": 2, "2": 3}//Initialize a dictionary}//goto statement func test Goto () {i: = 0Here://Declare a jump tag fmt. PRINTLN (i) if I < {I++goto here//jump to label}}//for statement func testfor () {var s [10]intfor J: = 0; J < Len (s); + J {S[j] = J//Array responsible for}for J: = 0; J < Len (s); J + + {FMT. PrIntln (S[j])//print each value of an array}//for+if statement for I: = 0; I < 100; i++ {if i%3 = = 0 && i%5! = 0 {fmt. Println ("Fizz")} else if i%5 = = 0 && i%3! = 0 {fmt. Println ("Bizz")} else if i%15 = = 0 {fmt. Println ("Fizzbizz")} else {fmt. Println (i)}}//prints a pyramid-shaped afor I: = 1; I <= 100; i++ {for J: = 1; J <= I; j + + {FMT. Printf ("%s", "A")}fmt. PRINTLN ()}}//traversal Mapfunc Eachmap () {mp2: = Make (Map[string]int)//map Another way of declaring mp2["one"] = 1mp2["two"] = 2for k, V: = range m P2 {fmt. Println ("Key:", K) fmt. Println ("Val:", v)}//only outputs the value of map for _, V: = range MP2 {fmt. Println ("Val:", v)}}//switch statement func Testswitch () {index: = 4switch Index {case 4:fmt. Println ("Hello 4") Fallthrough//enforces the following statement in case 5:fmt. Println ("Hello 5") default:fmt. PRINTLN ("Continue test")}}//uppercase method is public method, Func Test () {var arr [10]int//Declares a shape array of length 10 arr[0] = 10/assignment Arr[1] = 20arr2: = [3]in T{1, 2, 3}//Declare a shaped array of length 3 and assign a value of arr3: = [...] Int{2, 3, 4}//dynamically declares an array doublearray: = [2][2]int{[2]int{1, 2}, [2]int{3, 4}}                                     Declares a two-dimensional array easydouble: = [2][2]int{{1, 2}, {2, 3}}//two-dimensional array of another declarative way, internal and external type consistent var fslice []int                                  Declares a slice, and the array is removed from the length sslice: = []byte{1, 2, 3, 4}//Declare and initialize a slice tslice: = Arr2[0:2] The slice contains the arr[0],arr[1 in the array arr2], the Slice declares arr[n:m], and contains the elements from Arr[n to arr[m-1]fmt. PRINTLN ("test")}//the first letter lowercase method is the private method of the Func test () {FMT. PRINTLN ("Test")}

 

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.