1). File name & keyword & identifier
1. All go source codes end with. Go 2. Identifier begins with a letter or underscore, case sensitive 3. It is a special identifier to ignore the result. 4. Keep keywords
The golang keyword is as follows:
Calls to functions in the package:
a. functions in the same package can be called directly (case can be used) b. functions in different packages are called by package name + point + function name (the initial of function name must be uppercase)
Package access control rules:
a. capitalization means that the function / variable is exportable b. lowercase means that this function / variable is private and cannot be accessed outside the package
Example 1: write a program, for a given number n, find the combination of all the two adds equal to n.
The code is as follows:
package main import ( "fmt" ) func handle(n int){ for i := 0;i<=n;i++ { fmt.Printf("%d+%d=%d\n",i,n-i,n) // fmt.Printf() Is formatted output } } func main(){ handle(5) }
Compile run:
[root@NEO project]# go build -o bin/example01_plus go_dev/day02/example01_plus/main [root@NEO project]# bin/example01_plus 0+5=5 1+4=5 2+3=5 3+2=5 4+1=5 5+0=5 [root@NEO project]#
Example 2: a program contains two packages, add and main. There are two variables in the add package: Name and age. How to access Name and age in main package?
# The directory structure is as follows: [root@NEO day02]# tree example02_call_var01 example02_call_var01 ├── add │ └── add.go └── main └── main.go 2 directories, 2 files [root@NEO day02]# //The sample code is as follows: # Mode 1: main.go Document: package main import ( "go_dev/day02/example02_call_var01/add" "fmt" ) func main(){ fmt.Println("Name=",add.Name) fmt.Println("Age=",add.Age) } add.go Document: package add var Name string = "hello world" // Declare a variable and initialize the value of the variable (compile time); if a string is declared but not initialized, the value of the string variable is empty var Age int = 10 // String if declared but not initialized, the value of the string variable is 0 # The compiled run results are as follows: [root@NEO bin]# ./example0201 Name= hello world Age= 10 [root@NEO bin]# # Mode 2: main.go Document: package main import ( "go_dev/day02/example02_call_var02/add" "fmt" ) func main(){ add.Var_test() // Call this function first to Name and Age Initialize fmt.Println("Name=",add.Name) fmt.Println("Age=",add.Age) } add.go Document: package add var Name string var Age int func Var_test(){ // Capitalize the first letter Name = "hello world" // go It is a compiled language. All execution statements should be put into functions (assignment)./Initialization is also an execution statement) Age = 10 } # Error example: main.go Document content: package main import ( "go_dev/day02/example02_call_var03_err/add" "fmt" ) function main(){ fmt.Println("Name=",add.Name) fmt.Println("Age=",add.Age) } add.go Document content: package add var Name string var Age int Name = "hello world" // go It is a compiled language. All execution statements should be put into the function as the entry. Age = 10
Example 3: application of package alias: develop a program that uses package alias to access functions in package
# The main.go example is as follows: package main import ( a "go_dev/day02/example02_call_var01/add" // to go_dev/day02/example02_call_var01/add This name has an alias a "fmt" ) func main(){ fmt.Println("Name=",a.Name) // Use the package's alias to call functions in the package fmt.Println("Age=",a.Age) }
Example 4: each source file can contain an init function, which is automatically called by the go runtime framework. Write a program to demonstrate this function