table of Contents
table of Contents
@
Precautions
package main
It indicates that the current script is the program entry, containing the main functionIf not entrance, but simply a package, then try to keep it consistent with the name of the current directory folder represents a package of scripts
True slice of append function
slicez is a reference type when passed as a parameter, which is the address of the pass go, if made general changes that really changed
package main
import (
"fmt"
)
func ppp(s []int) {
s = append(s,3)
}
func main(){
s := make([]int,0)
fmt.Println(s)
ppp(s)
fmt.Println(s)
}
The result is:
[]
[]
Should have been changed, why not change?
Because the append function is not actually in the back plus some space, but again found a block of memory, while the original s did not move, ppp () which returns if, on the right meaning
it will modify when using the slice, it is recommended to make use of return value
Format function parameter time constant use, do not use string
package main
import (
"fmt"
"time"
)
func main() {
t := time.Now()
fmt.Println(t.Format(time.ANSIC))
fmt.Println(t.Format("Mon Jan _2 15:04:05 2006"))
}
The first line of output is recommended to use the second line do not know what kind of problem. . . balabala
range close attention in the bag use goroutine for: to pass parameters into account
func main() {
s := []string{"a", "b", "c"}
for _, v := range s {
go func() {
fmt.Println(v)
}()
}
select {}
}
The above results not get the results you want
func main() {
s := []string{"a", "b", "c"}
for _, v := range s {
go func(v string) {
fmt.Println(v)
}(v)
}
select {}
}
This code may be output, respectively a, b, c, but the order is different each time, look. .