01- print Hello World, variable

First, print Hello World

Here's what we wrote print hello world program

In the go language: // represents the single-line comment, / ** / on behalf of multi-line comments

// single line comment 
/ * 
multi-line comment 
multi-line comment 
* / 
Package main // indicates the current file belongs to the main package go
 Import  " fmt " // Import package fmt

 // compiled languages need a main function in the main package file entry 
main FUNC () { // define a main function
 
    fmt.Println ( " the Hello world " ) // Print helloworld, attention must use double quotes
     // a is the editor give you tips, the parameter name is a, only seen in the editor 

}

Operation mode:

1. Right-Run Project

2.go run project files

3.go build the project file ---> .exe file to perform the project

have to be aware of is:

1. introduced in the package must go use, such FMT, if not used will be given

2.print printed sure to use double quotes

Second, variable

The definition of a single variable

One way: Full Name (full)      
var variable type variable name = value 
, such as: var int A = 10      defines a type of int a, the 10 assigned to him. 

Second way: types derived var A
 = 10 

Three ways: brief statement 
a: = 10       

 

The sample code

main Package 

Import  " FMT " 

FUNC main () { 
    var Age int   // variable declaration, but no specific assignment 
    fmt.Println ( " My Age IS " , Age) // printing is 0 
    Age = 30 
    fmt.Println (Age) 
    Age = 50 
    fmt.Println (Age) 
} 

# results 
My Age IS 0
 30 
50

note:(******)

1 In the go language, you must use a variable definition, otherwise an error
 2. The third way is: can not write separately =
 3 variable name can not be repeated.
 4 .go is a strongly typed language, the type is fixed
 5 . If not only defines the initial value: int type default value is 0, string type defaults to an empty string 
var A int 
fmt.println (A)      # print is 0 
var B string 
fmt.println (B)       # print is empty

At the same time define multiple variables (four ways)

1. var a, b int = 10,20 # a, b are of type int 
2. var A, B = 10,20 
3. A, B: = 10,20 
. 4 var (. 
      Name = ' Bob ' 
      Age = 20 is 
      height = 180 [ 
)

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/wangcuican/p/12013097.html