Delete page "Go_tutorial"

李衍志523370910113 2024-10-14 19:28:05 +08:00
parent 7818e4af22
commit 3dfc6820db

@ -1,182 +0,0 @@
# Go_tutorial
[TOC]
This is a tutorial to guide the learning for the programming language Go.
## Structure
Let's first create a file called "Hello_JOJ.go", and write in:
```go
package main
import (
"fmt"
)
func main(){
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.
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
If you want to run the program above, you can either choose to directly run it:
```bash
$ go run Hello_JOJ.go
```
or choose to compile it as a binary file:
```bash
$ go build Hello_JOJ.go
$ ./Hello_JOJ
```
There are also many useful commands such as `go test` and `go get`. Look for official documents for help:
```bash
$ go help <topic>
```
## Data
### variables
#### Declaration
In Go, we have the keyword `var` to declare the variables. Here are some examples:
```go
var v1 int // define an integer
var v2 string // define a string
var v3 [10]int // define a array of int
var v4 []int // define a slice, a special data type in Go
var v5 struct {
f int
} // define a structure
var v6 *int // define pointer of 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
// In Go, function is an instance. It can be deliverd
// to a variable and be executed.
```
We can put all declarations together to make program clear and readable:
```go
var (
v1 int
v2 string
)
```
#### Initialization
There are three kinds of ways to initialize variables:
```go
var v1 int = 10 // method 1
var v2 = 10 // method 2
v3 := 10 // method 3
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.
#### 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:
```go
i, j = j, i
```
It makes function return values easier as well, which will be mentioned in the Function part.
#### 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:
```go
func GetName() (firstName, lastName, nickName string) {
return "May", "Chan", "Chibi Maruko"
}
_, _, nickName := GetName()
// We will only receive nickName and ingore
// the firstname and the lastname.
```
### Constant
#### declaration
Similar to C/C++, Go use the keyword `const` to declare constants.
```go
const v1 int = 10 // declare a constant integer
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.
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
const Home = os.GetEnv("HOME")
```
#### 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:
```go
const ( // iota reset to 0
c0 = iota // c0 == 0
c1 = iota // c1 == 1
c2 = iota // c2 == 2
)
const ( // iota reset to 0
a = 1 << iota // a == 1
b = 1 << iota // b == 2
c = 1 << iota // c == 4
)
const x = iota // x == 0
const y = iota // y == 0
```
If the expressions are same, we can ignore the last one. Therefore, we can simplify the code:
```go
const (
c0 = 1 << iota // c0 == 1
c1 // c1 == 2
c2 // c2 == 4
)
```
### Data type
There are many data types in go, which are listed below:
- bool
- int8, byte, int16, int, uint, uintptr
- float32, float64
- complex64. complex128
- string
- rune
- error
- pointer
- array
- slice
- map
- chan
- struct
- interface
## Goroutines and Channels