Update 'Go_tutorial'
parent
58754f3cd8
commit
a82b5455cc
364
Go_tutorial.md
364
Go_tutorial.md
|
|
@ -1,182 +1,182 @@
|
||||||
# Go_tutorial
|
# Go_tutorial
|
||||||
|
|
||||||
[TOC]
|
[TOC]
|
||||||
|
|
||||||
This is a tutorial to guide the learning for the programming language Go.
|
This is a tutorial to guide the learning for the programming language Go.
|
||||||
|
|
||||||
## Structure
|
## Structure
|
||||||
|
|
||||||
Let's first create a file called "Hello_JOJ.go", and write in:
|
Let's first create a file called "Hello_JOJ.go", and write in:
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main(){
|
func main(){
|
||||||
fmt.Println("Hello "+ "JOJ")
|
fmt.Println("Hello "+ "JOJ")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
This simple program contains 3 major elements: declare the package the program belongs to, import the dependent packages, and write the bodies of the program and functions. Each go app needs a main package.
|
This simple program contains 3 major elements: declare the package the program belongs to, import the dependent packages, and write the bodies of the program and functions. Each go app needs a main package.
|
||||||
|
|
||||||
Observe the structure it is similar to both Python and C++, as it needs to import certain libraries and some special packages to run the program. We will also see further that the syntax of Go is like the combination of both C++ and Python.
|
Observe the structure it is similar to both Python and C++, as it needs to import certain libraries and some special packages to run the program. We will also see further that the syntax of Go is like the combination of both C++ and Python.
|
||||||
|
|
||||||
## Compilation
|
## Compilation
|
||||||
|
|
||||||
If you want to run the program above, you can either choose to directly run it:
|
If you want to run the program above, you can either choose to directly run it:
|
||||||
```bash
|
```bash
|
||||||
$ go run Hello_JOJ.go
|
$ go run Hello_JOJ.go
|
||||||
```
|
```
|
||||||
or choose to compile it as a binary file:
|
or choose to compile it as a binary file:
|
||||||
```bash
|
```bash
|
||||||
$ go build Hello_JOJ.go
|
$ go build Hello_JOJ.go
|
||||||
$ ./Hello_JOJ
|
$ ./Hello_JOJ
|
||||||
```
|
```
|
||||||
|
|
||||||
There are also many useful commands such as `go test` and `go get`. Look for official documents for help:
|
There are also many useful commands such as `go test` and `go get`. Look for official documents for help:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
$ go help <topic>
|
$ go help <topic>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Data
|
## Data
|
||||||
|
|
||||||
### variables
|
### variables
|
||||||
|
|
||||||
#### Declaration
|
#### Declaration
|
||||||
|
|
||||||
In Go, we have the keyword `var` to declare the variables. Here are some examples:
|
In Go, we have the keyword `var` to declare the variables. Here are some examples:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var v1 int // define an integer
|
var v1 int // define an integer
|
||||||
var v2 string // define a string
|
var v2 string // define a string
|
||||||
var v3 [10]int // define a array of int
|
var v3 [10]int // define a array of int
|
||||||
var v4 []int // define a slice, a special data type in Go
|
var v4 []int // define a slice, a special data type in Go
|
||||||
var v5 struct {
|
var v5 struct {
|
||||||
f int
|
f int
|
||||||
} // define a structure
|
} // define a structure
|
||||||
var v6 *int // define pointer of int
|
var v6 *int // define pointer of int
|
||||||
var v7 map[string]int // define a map from string to int
|
var v7 map[string]int // define a map from string to int
|
||||||
var v8 func(a int) int // define a function get and return int
|
var v8 func(a int) int // define a function get and return int
|
||||||
// In Go, function is an instance. It can be deliverd
|
// In Go, function is an instance. It can be deliverd
|
||||||
// to a variable and be executed.
|
// to a variable and be executed.
|
||||||
```
|
```
|
||||||
|
|
||||||
We can put all declarations together to make program clear and readable:
|
We can put all declarations together to make program clear and readable:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var (
|
var (
|
||||||
v1 int
|
v1 int
|
||||||
v2 string
|
v2 string
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Initialization
|
#### Initialization
|
||||||
|
|
||||||
There are three kinds of ways to initialize variables:
|
There are three kinds of ways to initialize variables:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var v1 int = 10 // method 1
|
var v1 int = 10 // method 1
|
||||||
var v2 = 10 // method 2
|
var v2 = 10 // method 2
|
||||||
v3 := 10 // method 3
|
v3 := 10 // method 3
|
||||||
v4 := [3]int{1, 2, 3} //initialize a slice
|
v4 := [3]int{1, 2, 3} //initialize a slice
|
||||||
```
|
```
|
||||||
|
|
||||||
It can be observed that the compiler can deduce the data type automatically. However, when we use **method 3**, the variable should not be declared before, which will contribute to the error.
|
It can be observed that the compiler can deduce the data type automatically. However, when we use **method 3**, the variable should not be declared before, which will contribute to the error.
|
||||||
|
|
||||||
#### Assign value
|
#### Assign value
|
||||||
|
|
||||||
Similar to Python, Go provides a syntax sugar: **Multiple Assignment**. It allows you to assign values for several variables simultaneously. It can make your code simpler. For example, we can exchange values of two variables without a temporary variable:
|
Similar to Python, Go provides a syntax sugar: **Multiple Assignment**. It allows you to assign values for several variables simultaneously. It can make your code simpler. For example, we can exchange values of two variables without a temporary variable:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
i, j = j, i
|
i, j = j, i
|
||||||
```
|
```
|
||||||
|
|
||||||
It makes function return values easier as well, which will be mentioned in the Function part.
|
It makes function return values easier as well, which will be mentioned in the Function part.
|
||||||
|
|
||||||
#### Anonymous variable
|
#### Anonymous variable
|
||||||
|
|
||||||
It is common when we get several returning values from a function. However, if we want to ignore the unnecessaries, `_` can be used:
|
It is common when we get several returning values from a function. However, if we want to ignore the unnecessaries, `_` can be used:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func GetName() (firstName, lastName, nickName string) {
|
func GetName() (firstName, lastName, nickName string) {
|
||||||
return "May", "Chan", "Chibi Maruko"
|
return "May", "Chan", "Chibi Maruko"
|
||||||
}
|
}
|
||||||
_, _, nickName := GetName()
|
_, _, nickName := GetName()
|
||||||
// We will only receive nickName and ingore
|
// We will only receive nickName and ingore
|
||||||
// the firstname and the lastname.
|
// the firstname and the lastname.
|
||||||
```
|
```
|
||||||
|
|
||||||
### Constant
|
### Constant
|
||||||
|
|
||||||
#### declaration
|
#### declaration
|
||||||
|
|
||||||
Similar to C/C++, Go use the keyword `const` to declare constants.
|
Similar to C/C++, Go use the keyword `const` to declare constants.
|
||||||
|
|
||||||
```go
|
```go
|
||||||
const v1 int = 10 // declare a constant integer
|
const v1 int = 10 // declare a constant integer
|
||||||
const v2 = 10 // same as the previous one
|
const v2 = 10 // same as the previous one
|
||||||
```
|
```
|
||||||
|
|
||||||
Declaring the data type of a constant is not necessary. If not, they are untyped constants, the same as literal constants.
|
Declaring the data type of a constant is not necessary. If not, they are untyped constants, the same as literal constants.
|
||||||
|
|
||||||
The value of constants should be assigned before or during compilation, which means assigning constant expressions to constants is acceptable. However, the expression that gets values when running will lead to an error. For example:
|
The value of constants should be assigned before or during compilation, which means assigning constant expressions to constants is acceptable. However, the expression that gets values when running will lead to an error. For example:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
const Home = os.GetEnv("HOME")
|
const Home = os.GetEnv("HOME")
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Iota
|
#### Iota
|
||||||
|
|
||||||
Go predefines a constant `iota`. Each time a `const` keyword appears, it is reset to 0, and then before the next `const`, the number represented by `iota` is automatically increased by 1 for each occurrence of `iota`.
|
Go predefines a constant `iota`. Each time a `const` keyword appears, it is reset to 0, and then before the next `const`, the number represented by `iota` is automatically increased by 1 for each occurrence of `iota`.
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
const ( // iota reset to 0
|
const ( // iota reset to 0
|
||||||
c0 = iota // c0 == 0
|
c0 = iota // c0 == 0
|
||||||
c1 = iota // c1 == 1
|
c1 = iota // c1 == 1
|
||||||
c2 = iota // c2 == 2
|
c2 = iota // c2 == 2
|
||||||
)
|
)
|
||||||
|
|
||||||
const ( // iota reset to 0
|
const ( // iota reset to 0
|
||||||
a = 1 << iota // a == 1
|
a = 1 << iota // a == 1
|
||||||
b = 1 << iota // b == 2
|
b = 1 << iota // b == 2
|
||||||
c = 1 << iota // c == 4
|
c = 1 << iota // c == 4
|
||||||
)
|
)
|
||||||
const x = iota // x == 0
|
const x = iota // x == 0
|
||||||
const y = iota // y == 0
|
const y = iota // y == 0
|
||||||
```
|
```
|
||||||
|
|
||||||
If the expressions are same, we can ignore the last one. Therefore, we can simplify the code:
|
If the expressions are same, we can ignore the last one. Therefore, we can simplify the code:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
const (
|
const (
|
||||||
c0 = 1 << iota // c0 == 1
|
c0 = 1 << iota // c0 == 1
|
||||||
c1 // c1 == 2
|
c1 // c1 == 2
|
||||||
c2 // c2 == 4
|
c2 // c2 == 4
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Data type
|
### Data type
|
||||||
|
|
||||||
There are many data types in go, which are listed below:
|
There are many data types in go, which are listed below:
|
||||||
|
|
||||||
- bool
|
- bool
|
||||||
- int8, byte, int16, int, uint, uintptr
|
- int8, byte, int16, int, uint, uintptr
|
||||||
- float32, float64
|
- float32, float64
|
||||||
- complex64. complex128
|
- complex64. complex128
|
||||||
- string
|
- string
|
||||||
- rune
|
- rune
|
||||||
- error
|
- error
|
||||||
- pointer
|
- pointer
|
||||||
- array
|
- array
|
||||||
- slice
|
- slice
|
||||||
- map
|
- map
|
||||||
- chan
|
- chan
|
||||||
- struct
|
- struct
|
||||||
- interface
|
- interface
|
||||||
|
|
||||||
|
## Goroutines and Channels
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user