"Go_tutorial" updated

ssanakkana 2024-05-14 00:31:17 +08:00
parent 983348ebcd
commit d83d9d73f9

@ -18,9 +18,9 @@ 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.
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 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.
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
@ -46,7 +46,7 @@ $ go help <topic>
#### Declaration
In Go, we have 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
var v1 int // define an integer
@ -74,7 +74,7 @@ var (
#### Initialization
There are three kinds of way to initialize variables:
There are three kinds of ways to initialize variables:
```go
var v1 int = 10 // method 1
@ -82,19 +82,19 @@ 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.
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 symutaneously. It can make your code simpler. For example, we can exchange values of two variables without 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
i, j = j, i
```
It makes function return values more easier as well, which will mention in Function part.
It makes function return values easier as well, which will be mentioned in the Function part.
#### Annoymous 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:
@ -111,16 +111,16 @@ _, _, nickName := GetName()
#### declaration
Similar to C/C++, Go use keyword `const` to declare constants.
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 neccessary. If not, they are untyped const ants, 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 complination, which means assigning constant experssion to constants is acceptable. However, the expression which get values when running will lead to 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
const Home = os.GetEnv("HOME")
@ -148,7 +148,7 @@ 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:
If the expressions are same, we can ignore the last one. Therefore, we can simplify the code:
```go
const (
@ -158,5 +158,24 @@ const (
)
```
### 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