This example describes the Go language basics. Share to everyone for your reference, specific as follows:
Go file directory structure
The diagram is the directory structure of the Go-windows
As pictured, the Go program should be under the Gopath folder, divided into bin, pkg, and SRC three subfolders
SRC folder: Each subordinate folder (such as demo) represents a go item, which holds the source program
Bin folder: A binary executable program that is generated for each project on go. An. exe file is generated under Windows, and an executable file is generated under Linux. One of the biggest features of Go is portability, which means that when a demo.exe is generated, it can be performed on any Windows system (even if no Go installer package is installed). This is to let Php,python and other scripting languages look Mo and.
Pkg folder: third-party library. It contains the Third-party libraries (unofficial already provided libraries) that are referenced in your project.
Go Basics
Variable assignment: (in the following forms, set a to int 12)
Copy Code code as follows:
var a
A = 12
A: = 12
A,b: =12,23
constant Assignment:
1 Itoa Use
Copy Code code as follows:
Iota is the constant assignment increment of go, the code above sets a to 0,b set to 1
2 string
The string in go is a constant and can only be expressed in double quotes.
A: = "This is string"
A[0] = ' C ' (this is wrong, will complain)
If you want to do the above you should do this:
Copy Code code as follows:
A: = "This is string"
c: = []bytes (a)
C[0] = ' C '
D: = string (c)
3 Circulation and selection structure
There is no doing while in the go, the loop structure is only for. Select structure has if and switch
If statement
Copy Code code as follows:
If err: = file. Chmod (777); Err!= Nil {
return err
}
Cut knot: The opening parenthesis must be on the same line as if, so
Copy Code code as follows:
If err: = file. Chmod (777); Err!= Nil
{//This is illegal in the GO statement
return err
}
For loop
Copy Code code as follows:
Sum: = 0
For I: = 0; I < 10; i++ {
sum = i
}
List: = []string{"AAA", "BBB", "CCC"}
For _,v: = Range list{
Fmt. Print ("%s", V)
}
Here we use a range,key and value of int and string respectively, key is ordinal, starting from 0, value is
Switch statement
switch is similar to other languages, only one, it does not need to break, query to a satisfying condition, execute, and then jump out
Copy Code code as follows:
Switch a {
Case "Test1":
Fmt. Print ("Test1")
Case "Test2", "test3":
Fmt. Print ("Testohter")
Default
Fmt. Print ("Notest")
}
Array, slice, and map
The array is the one that you normally use, and the array in the C language is the same
Copy Code code as follows:
var a [10]int
A[0] = 1
A[1] = 2
Two-dimensional arrays:
Copy Code code as follows:
A: = [2][2]int{{1,2}, {3,4}}
Slice and array approach, slice understood as pointers to array, using make for memory allocation
Copy Code code as follows:
The description of the conversion of array and slice uses excerpts from the Learning Go language:
Want to extend the Slice,append and copy two built-in functions.
(PS: The difference between a built-in function and a Third-party library function is that the built-in function starts with a lowercase letter, such as copy (), and a third-party library function is a first-letter capitalization, such as FMT. Print ())
The map structure is a hash map
Copy Code code as follows:
Ages: = map[string]int {
"Lili": 13,
"Nick": 23,
"Jacky": 55,
}
Notice here that in many languages the last comma is always required to omit (the comma after 55), but this comma is required in the go language when listing such constructs.
Exercises:
1 Create a simple loop based on for. Make it loop 10 times and print out the counter's value using the FMT package.
2 use Goto to modify 1 of the loop, do not use for
3 rewrite the 1 loop again so that it traverses an array and prints the array to the screen
4 Write a reverse string of procedures, such as: "Foobar" Printing to become "raboof";
To post my answer:
(On the fourth question, there are many kinds of solutions, more detailed please see: http://stackoverflow.com/questions/1752414/how-to-reverse-a-string-in-go)
Copy Code code as follows:
Package Main
Import (
"FMT"
)
Func Main () {
Forexample ()
Fmt. Println ("----------------------")
Goexample ()
Fmt. Println ("----------------------")
Arrexample ()
Fmt. Println ("----------------------")
Revert ("Testrevert hah")
}
Func forexample () {
For i:= 0; I < 10; i++ {
Fmt. Println (i)
}
}
Func goexample () {
I: = 0
I:
Fmt. Println (i)
i++
if (I < 10) {
Goto I
}
}
Func arrexample () {
Arr: = [10]int{0,1,2,3,4,5,6,7,8,9}
For _,val: = Range arr{
Fmt. Println (Val)
}
}
Func Revert (s string) {
var result string
For _,val: = Range s{
result = String (val) + result
}
Fmt. PRINTLN (Result)
}
I hope this article will help you with your go language program.