Go language Learning one: basic statements

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Installation Sublime Reference: http://blog.csdn.net/cyxcw1/article/details/10329481


This series of references: http://www.runoob.com/go/go-ide.html has instructions for installing development tools

Note: The above registration code is not available in the sublime3.0 version

First Program

The first go program, note that there is no semicolon end,//I practical is sublime as the editor//package main represents a program that can be executed independently, each Go application contains a package called Main. Package main//This line of code defines the packet name//tells the Go compiler that this program needs to use the FMT package (the function, or other element), the FMT package implements the format IO (input/output) function. Import (<span style= "White-space:pre" ></span> "FMT")/* when identifiers (including constants, variables, types, function names, structure fields, and so on) start with an uppercase letter, such as: Group1, An object using this form of identifier can then be used by the code of the external package (the client program needs to import the package first), which is known as an export (like public in an object-oriented language); If the identifier starts with a lowercase letter, it is not visible outside the package. But they are visible and usable within the entire package (like private in the object-oriented language). *///main function, each program must have func main () {<span style= "white-space:pre" ></span>/* Comment: This is my first simple program */<span style= " White-space:pre "></span>fmt. Println ("hello,world!") You can output a string to the console and automatically increment the newline character at the end. \ n}

Because this is the first program, so say how to compile a run

Press Ctrl+b to write to the command line:

Go Run test.go
can be executed.

Package Mainimport ("FMT") func main () {var int//definition variable var identifier type//similar uint8,256//variable declaration//First var wo string//specify variable type , use the default value if you do not assign a value after the declaration. wo= "Shawn"///second Var woshi= "Shawn"//Compiler Auto-discriminant//Third ellipsis var,:= front cannot be declared, this is a special tag woshishui:= "Shawn"//Multivariable declaration, and the preceding one, As long as a comma, the following var ni,wo,tani,wo ta=v1,v2,v3////This factorization keyword is generally used to declare a global variable \var (name Stringage uint8)//This can only appear in the body of the function without a declarative format//g , h: = 123, "Hello"//value type and reference type//go language constant//explicit definition const name string= "Shawn"//implicitly defines const NAME2 = "ABC"//constant can also enumerate, where if the function value is constant, it can only be inside The Set function const (unknown=0female=1male=2)//iota, a special constant, can be considered a constant that can be modified by the compiler.    When each const keyword appears, it is reset to 0, and before the next const appears, each time a iota appears, the number it represents will automatically increase by 1. Iota can be used as an enumeration value://const (///A=iota//B=iota//C=iota//)/const (I=1<<iota//J=3&L    T;<iota//3 Move left one k//the following two lines default to K=3<<2,l=3<<3. L)}func Fun () {var a string = "abc"//declare local variable must be used a= "Shawn"//Simple assignment also not FMT. Println ("Hello, World", a)//this will be better/* But global variables are allowed to be declared but not used. "ctrl+shift+/" can be commented */}//ctrl+/can have "//" such comments, repeat can cancel//if you want to exchange the value of two variables, you can simplyUse a, B = B, A. The blank identifier _ is also used to discard values, such as the value 5 in: _, B = 5, 7 is discarded. _ is actually a write-only variable, you can't get its value. This is done because you have to use all the declared variables in the//Go language, but sometimes you don't need to use all the return values from a function. Parallel assignments are also used when a function returns multiple return values, such as Val and error err in this case: val, err = Func1 (var1) by calling the FUNC1 function.







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.