Update "Go_tutorial"

ssanakkana 2024-05-12 23:59:11 +08:00
parent 582624c28d
commit 983348ebcd

@ -1,6 +1,11 @@
# Go_tutorial
[TOC]
This is a tutorial to guide the learning for the programming language Go.
## Structure and Compilation
## Structure
Let's first create a file called "Hello_JOJ.go", and write in:
```go
package main
@ -13,9 +18,13 @@ func main(){
fmt.Println("Hello "+ "JOJ")
}
```
This simple programme contains 3 major elements: declare the package the programme belong to, import the dependent packages, and write the bodies of the programme 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 library and need some special package to run the program. We will also see further that the syntax of Go is like the combination of both C++ and Python.
If you want to run the program, you can either choose to directly run it:
## Compilation
If you want to run the program above, you can either choose to directly run it:
```bash
$ go run Hello_JOJ.go
```
@ -23,4 +32,131 @@ 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 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 way to initialize variables:
```go
var v1 int = 10 // method 1
var v2 = 10 // method 2
v3 := 10 // method 3
```
It can be observed that complier can deduce the data type automatically. However, when we use **method 3**, the variable should not be declared before, which will contibute to the error.
#### Assign value
Similar to Python, Go provides a syntax sugar: **Multiple Assignment**. It allows you to assign values for several variables symutaneously. It can make your code simpler. For example, we can exchange values of two variables without temporary variable:
```go
i, j = j, i
```
It makes function return values more easier as well, which will mention in Function part.
#### Annoymous 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 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 neccessary. If not, they are untyped const ants, same as literal constants.
The value of constants should be assigned before or during complination, which means assigning constant experssion to constants is acceptable. However, the expression which get values when running will lead to 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 expression 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
)
```