This is a creation in Article, where the information may have evolved or changed.
Ha ha
Package Main//keyword VAR declares variable type information after the variable name//declaring a variable of type intvarV1int//declares a string typevarV2string//declaring multiple variablesvarV3, v4BOOL//[0 0 0 0 0 0 0 0 0 0] ArrayvarV5 [Ten]int//array slicesvarV6 []int//declaring struct bodiesvarV7struct{fint}//declaring pointersvarV8 *int//declares that map key is of type string and value is of type intvarV9 map[string]int//anonymous functions and closuresvarV10 func (Aint)int//multiple variables that need to be declared are put togethervar(Nameint Agestring)
declaring Variables
Package Mainimport"FMT"Func Main () {//declaring variable initialization, VAR can omit the notation varV1int=Ten //the compiler can automatically calculate the type of V2 varV2 =Ten //The compiler automatically calculates the type of the V3//simultaneous declaration and initialization of variablesV3: =TenFMT. Println (v1, V2, v3)}//out now: The variable on the left can only be declared once and cannot be duplicated
Initialization of variables
package mainimport " FMT // The declaration is then assigned a value of func Main () { var i int i = 100 var J int J = 50 // go the multiple assignments of the language, the following code exchanges I and J variables Span style= "color: #008000;" >// go's multi-assignment attribute can be significantly optimized for code, compared to C + + I, J = J, I fmt. Println (i, j)}
assigning values to variables
Package Mainimport"FMT"//when programming with a strongly typed language, when a function is called in order to get a value, but because the function returns multiple values, you have to define a bunch of variables//You can use multiple returns and anonymous variables to avoid these problemsFunc GetName () (FirstName, LastName, nicknamestring) { return "Yu","Yuchao","Chaoge"}//just want to get nickname, function call statement can writeFunc Main () {//optimize the clarity of your code_, _, Nickname: =GetName () fmt. Println (nickname)}
Anonymous Variables
Package Main//declare the package that the go code belongs to, the package is the most basic distribution unit of go, to generate the executable program, must be named Main, and there is a main () function, as the starting point of execution//After importing the package from this program, the following is used to println () function, so import FMTImport"FMT"//do not write unused packages in source code, otherwise the compiler will error//The design philosophy of software engineering, forcing the placement of the left curly brace {//casing Rules for function namesFunc Compute (value1int, value2 float64) (Resule float64, err Error) { //function BodyFMT. Println (value1, value2)return}//The main function cannot have parameters, nor can it define return values, and the command line passed in parameters that exist on the OS. In the args variableFunc Main () {Compute (1,3)}
Code Interpretation
Package Main//constants are known and immutable during compilation, and can be numeric types (integer, float, complex), Boolean, StringConstPi float64 =3.14159265358979323846//no type floating-point constantsConstZero =0.0Const ( //No type constantSize Int64 =1024x768EOF= -1)//constant Multiple assignment u=0.0 v=3.0ConstU, v float32 =0,3//Untyped Integer, string constantConstA, b, C =3,4,"Foo"
Constants