Go language Preliminary understanding

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.
//myNote1 project Main.go PackageMaindifferent packages are available in the//go language, equivalent to different classes in Java to provide functionalityImport("FMT")var(//This factorization keyword is commonly used to declare global variablesAaintBbBOOL)funcMain () {//Output statementFmt. Println ("Hello world!")//define Variables    varIinti =Ten    varj ="JJJJJJJJJJJJ"K: ="KKKKKKKKKKK"Fmt. Println ("Variable:", i) fmt. Println ("Variable:", j) Fmt. Println ("Variable:"K//define multiple variables    varS, u, gintS, u, g =Ten, -, -Fmt. Println ("Variable:", s) fmt. Println ("Variable:", u) fmt. Println ("Variable:", g)varA, b, C ="AAA","BBB","CCC"Fmt. Println ("Variable:", a) fmt. Println ("Variable:", b) fmt. Println ("Variable:", c) d, E, f: ="DDD","Eee","FFF"Fmt. Println ("Variable:", d) fmt. Println ("Variable:", e) fmt. Println ("Variable:", f)//global variableFmt. Println ("global variable:", AA) FMT. Println ("global variable:", BB)//Get the memory address of the variableFmt. Println ("Memory address:", &a)//When a variable is declared, but it is not used, it will be reported to JJ declared and not used    //var JJ string = "JK"    //Define constants programming habits are defined in uppercase letters .    //const defining constants of a specific type    ConstLeftint= -    Constright = $    ConstUp, down = -, -    Const(before = -after = -) FMT. Println ("constant:", left, right, up, down, before, after)//Operations    varV, Zint=Ten, -Fmt. Println ("Addition:", V+z) fmt. Println ("Subtraction:", V-z) fmt. Println ("Multiplication:", V*z) fmt. Println ("Division:", v/z) fmt. Println ("Take the remainder:", V%z) fmt. Println ("Less than equals:", v <= z) fmt. Println ("Equals:", v = = z)//if Else ElseIf    ifTen==Ten{FMT. Println ("if statement")    }ifTen== -{FMT. Println ("if statement")    }Else{FMT. Println ("Else Statement")    }//switchJK: ="a"    SwitchJK { Case "a": FMT. Println ("Branch Statement") Break     Case "B": FMT. Println ("xxxxx") Break    default: FMT. Println ("--------") Break}varindex =1     forIndex <4{FMT. Println ("-------") index++}varFlatint=3A:index =1     forIndex <3{FMT. Println ("xxxx") index++} flat++ifFlat <5{GotoA} testFun1 () testFun2(Ten, -) FMT. Println ("function returns:", TESTFUN3(Ten, -)) x, y: = TestFun4(Ten, -) FMT. Println ("function returns multiple values:", X, y)varArr[3]intArr[0] =1Arr[1] =TenArr[2] = +{varOpint=0         forOp <Len(arr) {FMT. Println (Arr[op]) op++}}//Do not set the size of the array    varARR1 = [...]float64{12.12,34.2,45.2} FMT. Println (ARR1)varARR2 =[5]float32{11.23,343.23,34.5} FMT. Println (ARR2)//Data structure pointers    varInchint= -Fmt. Println ("Address of the variable", &in)//Defines a pointer to an integer    varINI *intINI = &in FMT. Println ("pointer fetch data", *ini)//null pointer    varInk *intFmt. Println ("Judging null pointer", (Ink = =Nil))The //array can store the same type of data, but in structs we can define different data types for different items. Plainly, a class in Java that creates a template for an object.    typeBookstruct{TitlestringNumintnames []float32FlagBOOL}varBook1 Book Book1.flag =trueBook1.num =TenBook1.title ="Cao"Book1.names = []float32{1000.0,2.0,3.4,7.0,50.0} FMT. Println (Book1)The length of the//go array is immutable, and in a particular scenario such a collection is less applicable, and Go provides a flexible,    //powerful built-in type slicing ("Dynamic array"), the length of slices compared to arrays is not fixed, you can append elements, may increase the capacity of slices when appending.     //White is a collection in Java    //Slice use make to create: T represents the type of slice element being created.     the//function make accepts a type, a length (the initialization length when the tile is created), and an optional capacity parameter. When make is called, an array is allocated internally and the corresponding slice of the array is returned.     //var numbers []int = make ([]int, 5, 5)Numbers: = []int{0,1,2,3,4,5,6,7,8,9,Ten, One, A} FMT. PRINTLN (Numbers) fmt. Println ("numbers[1:4] = =", numbers[1: 4])/ * Default lower limit is 0*/Fmt. Println ("numbers[:3] = =", numbers[: 3])/* The default upper limit is Len (s) */Fmt. Println ("numbers[4:] = =", numbers[4:]) fmt. Println ("Calculate the tile's capacity:",Cap(numbers)) Fmt. Println ("Calculate the length of the slice:",Len(numbers)) Numbers =Append(Numbers, -) FMT. Println (after appending the element:, numbers)//Remove the element labeled 9Numbers =Append(numbers[: 9], numbers[Ten:]...) Fmt. Println ("After deleting the element at the specified location:", numbers)//Delete the last element of a sliceNumbers =Append(numbers[:Len(Numbers)-1], numbers[Len(numbers):] ...) Fmt. Println ("Delete the last element of a slice:", numbers) fmt. Println ("Calculate the length of the slice:",Len(numbers))varNumber1 []int    ifNumber1 = =Nil{FMT. Println ("Slice is empty")    }//Create map    varCountrycapitalmapMap[string]string    / * Create a collection * /Countrycapitalmap = Make(Map[string]string)/ * Map Insert Key-value pairs, each country corresponding to the capital * *countrycapitalmap["France"] ="Paris"countrycapitalmap["Italy"] ="Rome"countrycapitalmap["Japan"] ="Tokyo"countrycapitalmap["India"] ="New Delhi"Fmt. Println (countrycapitalmap["India"])/ * Use key to output map values * /     forCountry: =RangeCountrycapitalmap {fmt. Println (" Capital of", Country,"is", Countrycapitalmap[country])}/ * To see if the element exists in the collection * /Captial, OK: = countrycapitalmap["India"]/ * If OK is true, then exists, otherwise does not exist * /    ifOK {fmt. Println ("Capital of India is----", Captial)}Else{FMT. Println ("Capital of states are not present")    }//Implement two structural bodies    varPhone Phone phone =New(IPhone) phone.call () phone =New(Xiaomi) Phone.call ()}//functionfuncTestFun1 () {fmt. Println ("function")}funcTestFun2 (NUM1int, num2int) {FMT. Println ("function parameter", num1+num2)}//function returns a single valuefuncTESTFUN3 (NUM1int, num2int)int{returnNUM1 + num2}//function returns multiple valuesfuncTestFun4 (NUM1int, num2int) (int,int) {returnNUM1 + num2, num1-num2}//define interfaces use different structures to create different methods to inherit the interfacetypePhoneInterface{//methods in the interfaceCall ()}//define Structure bodytypeIphonestruct{}//define the methods in the interfacefunc(iphone iphone) call () {FMT. Println ("The phone you use is an apple.")}//define another structure bodytypeXiaomistruct{}//define the methods in the interfacefunc(Xiaomi Xiaomi) call () {FMT. Println ("You use the phone is millet:")}
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.