Fixed formatting
While reading the PDF version it becomes difficult to make sense of the stuff which has a horizontal scroll bar, so brought everything to one line so we won't have problem in reading a pdf version
This commit is contained in:
committed by
James Miranda
parent
cac19e592d
commit
a0794a2962
58
en/02.2.md
58
en/02.2.md
@@ -29,7 +29,9 @@ Define multiple variables with initial values.
|
||||
*/
|
||||
var vname1, vname2, vname3 type = v1, v2, v3
|
||||
|
||||
Do you think that it's too tedious to define variables use the way above? Don't worry, because the Go team has also found this to be a problem. Therefore if you want to define variables with initial values, we can just omit the variable type, so the code will look like this instead:
|
||||
Do you think that it's too tedious to define variables use the way above? Don't worry, because the Go team has also found
|
||||
this to be a problem. Therefore if you want to define variables with initial values, we can just omit the variable type,
|
||||
so the code will look like this instead:
|
||||
|
||||
/*
|
||||
Define three variables without type "type", and initialize their values.
|
||||
@@ -224,7 +226,8 @@ Go has one keyword called `iota`, this keyword is to make `enum`, it begins with
|
||||
x = iota // x == 0
|
||||
y = iota // y == 1
|
||||
z = iota // z == 2
|
||||
w // If there is no expression after the constants name, it uses the last expression, so it's saying w = iota implicitly. Therefore w == 3, and y and z both can omit "= iota" as well.
|
||||
w // If there is no expression after the constants name, it uses the last expression,
|
||||
//so it's saying w = iota implicitly. Therefore w == 3, and y and z both can omit "= iota" as well.
|
||||
)
|
||||
|
||||
const v = iota // once iota meets keyword `const`, it resets to `0`, so v = 0.
|
||||
@@ -253,8 +256,10 @@ in `[n]type`, `n` is the length of the array, `type` is the type of its elements
|
||||
var arr [10]int // an array of type [10]int
|
||||
arr[0] = 42 // array is 0-based
|
||||
arr[1] = 13 // assign value to element
|
||||
fmt.Printf("The first element is %d\n", arr[0]) // get element value, it returns 42
|
||||
fmt.Printf("The last element is %d\n", arr[9]) //it returns default value of 10th element in this array, which is 0 in this case.
|
||||
fmt.Printf("The first element is %d\n", arr[0])
|
||||
// get element value, it returns 42
|
||||
fmt.Printf("The last element is %d\n", arr[9])
|
||||
//it returns default value of 10th element in this array, which is 0 in this case.
|
||||
|
||||
Because length is a part of the array type, `[3]int` and `[4]int` are different types, so we cannot change the length of arrays. When you use arrays as arguments, functions get their copies instead of references! If you want to use references, you may want to use `slice`. We'll talk about later.
|
||||
|
||||
@@ -262,7 +267,9 @@ It's possible to use `:=` when you define arrays.
|
||||
|
||||
a := [3]int{1, 2, 3} // define an int array with 3 elements
|
||||
|
||||
b := [10]int{1, 2, 3} // define a int array with 10 elements, of which the first three are assigned. The rest of them use the default value 0.
|
||||
b := [10]int{1, 2, 3}
|
||||
// define a int array with 10 elements, of which the first three are assigned.
|
||||
//The rest of them use the default value 0.
|
||||
|
||||
c := [...]int{4, 5, 6} // use `…` to replace the length parameter and Go will calculate it for you.
|
||||
|
||||
@@ -293,7 +300,9 @@ Then we define a `slice`, and initialize its data.
|
||||
|
||||
slice := []byte {'a', 'b', 'c', 'd'}
|
||||
|
||||
`slice` can redefine existing slices or arrays. `slice` uses `array[i:j]` to slice, where `i` is the start index and `j` is end index, but notice that `array[j]` will not be sliced since the length of the slice is `j-i`.
|
||||
`slice` can redefine existing slices or arrays. `slice` uses `array[i:j]` to slice, where `i` is
|
||||
the start index and `j` is end index, but notice that `array[j]` will not be sliced since the length
|
||||
of the slice is `j-i`.
|
||||
|
||||
// define an array with 10 elements whose types are bytes
|
||||
var ar = [10]byte {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}
|
||||
@@ -309,7 +318,8 @@ Then we define a `slice`, and initialize its data.
|
||||
b = ar[3:5]
|
||||
// now 'b' has elements ar[3] and ar[4]
|
||||
|
||||
Notice the differences between `slice` and `array` when you define them. We use `[…]` to let Go calculate length but use `[]` to define slice only.
|
||||
Notice the differences between `slice` and `array` when you define them. We use `[…]` to let Go
|
||||
calculate length but use `[]` to define slice only.
|
||||
|
||||
Their underlying data structure.
|
||||
|
||||
@@ -342,7 +352,9 @@ More examples pertaining to `slice`
|
||||
bSlice = aSlice[0:5] // slice could be expanded in range of cap, now bSlice contains d,e,f,g,h
|
||||
bSlice = aSlice[:] // bSlice has same elements as aSlice does, which are d,e,f,g
|
||||
|
||||
`slice` is a reference type, so any changes will affect other variables pointing to the same slice or array. For instance, in the case of `aSlice` and `bSlice` above, if you change the value of an element in `aSlice`, `bSlice` will be changed as well.
|
||||
`slice` is a reference type, so any changes will affect other variables pointing to the same slice or array.
|
||||
For instance, in the case of `aSlice` and `bSlice` above, if you change the value of an element in `aSlice`,
|
||||
`bSlice` will be changed as well.
|
||||
|
||||
`slice` is like a struct by definition and it contains 3 parts.
|
||||
|
||||
@@ -366,13 +378,17 @@ There are some built-in functions for slice.
|
||||
- `append` appends one or more elements to `slice`, and returns `slice` .
|
||||
- `copy` copies elements from one slice to the other, and returns the number of elements that were copied.
|
||||
|
||||
Attention: `append` will change the array that `slice` points to, and affect other slices that point to the same array. Also, if there is not enough length for the slice (`(cap-len) == 0`), `append` returns a new array for this slice. When this happens, other slices pointing to the old array will not be affected.
|
||||
Attention: `append` will change the array that `slice` points to, and affect other slices that point to the same array.
|
||||
Also, if there is not enough length for the slice (`(cap-len) == 0`), `append` returns a new array for this slice. When
|
||||
this happens, other slices pointing to the old array will not be affected.
|
||||
|
||||
### map
|
||||
|
||||
`map` behaves like a dictionary in Python. Use the form `map[keyType]valueType` to define it.
|
||||
|
||||
Let's see some code. The 'set' and 'get' values in `map` are similar to `slice`, however the index in `slice` can only be of type 'int' while `map` can use much more than that: for example `int`, `string`, or whatever you want. Also, they are all able to use `==` and `!=` to compare values.
|
||||
Let's see some code. The 'set' and 'get' values in `map` are similar to `slice`, however the index in `slice` can only be
|
||||
of type 'int' while `map` can use much more than that: for example `int`, `string`, or whatever you want. Also, they are
|
||||
all able to use `==` and `!=` to compare values.
|
||||
|
||||
// use string as the key type, int as the value type, and `make` initialize it.
|
||||
var numbers map[string] int
|
||||
@@ -398,17 +414,19 @@ Use `delete` to delete an element in `map`.
|
||||
|
||||
// Initialize a map
|
||||
rating := map[string]float32 {"C":5, "Go":4.5, "Python":4.5, "C++":2 }
|
||||
// map has two return values. For the second return value, if the key doesn't exist,'ok' returns false. It returns true otherwise.
|
||||
// map has two return values. For the second return value, if the key doesn't
|
||||
//exist,'ok' returns false. It returns true otherwise.
|
||||
csharpRating, ok := rating["C#"]
|
||||
if ok {
|
||||
fmt.Println("C# is in the map and its rating is ", csharpRating)
|
||||
fmt.Println("C# is in the map and its rating is ", csharpRating)
|
||||
} else {
|
||||
fmt.Println("We have no rating associated with C# in the map")
|
||||
fmt.Println("We have no rating associated with C# in the map")
|
||||
}
|
||||
|
||||
delete(rating, "C") // delete element with key "c"
|
||||
|
||||
As I said above, `map` is a reference type. If two `map`s point to same underlying data, any change will affect both of them.
|
||||
As I said above, `map` is a reference type. If two `map`s point to same underlying data,
|
||||
any change will affect both of them.
|
||||
|
||||
m := make(map[string]string)
|
||||
m["Hello"] = "Bonjour"
|
||||
@@ -417,13 +435,19 @@ As I said above, `map` is a reference type. If two `map`s point to same underlyi
|
||||
|
||||
### make, new
|
||||
|
||||
`make` does memory allocation for built-in models, such as `map`, `slice`, and `channel`, while `new` is for types' memory allocation.
|
||||
`make` does memory allocation for built-in models, such as `map`, `slice`, and `channel`, while `new` is for types'
|
||||
memory allocation.
|
||||
|
||||
`new(T)` allocates zero-value to type `T`'s memory, returns its memory address, which is the value of type `*T`. By Go's definition, it returns a pointer which points to type `T`'s zero-value.
|
||||
`new(T)` allocates zero-value to type `T`'s memory, returns its memory address, which is the value of type `*T`. By Go's
|
||||
definition, it returns a pointer which points to type `T`'s zero-value.
|
||||
|
||||
`new` returns pointers.
|
||||
|
||||
The built-in function `make(T, args)` has different purposes than `new(T)`. `make` can be used for `slice`, `map`, and `channel`, and returns a type `T` with an initial value. The reason for doing this is because the underlying data of these three types must be initialized before they point to them. For example, a `slice` contains a pointer that points to the underlying `array`, length and capacity. Before these data are initialized, `slice` is `nil`, so for `slice`, `map` and `channel`, `make` initializes their underlying data and assigns some suitable values.
|
||||
The built-in function `make(T, args)` has different purposes than `new(T)`. `make` can be used for `slice`, `map`,
|
||||
and `channel`, and returns a type `T` with an initial value. The reason for doing this is because the underlying data of
|
||||
these three types must be initialized before they point to them. For example, a `slice` contains a pointer that points to
|
||||
the underlying `array`, length and capacity. Before these data are initialized, `slice` is `nil`, so for `slice`, `map`
|
||||
and `channel`, `make` initializes their underlying data and assigns some suitable values.
|
||||
|
||||
`make` returns non-zero values.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user