Golang中遇到的[坑]
Contents
匿名函数
package main
import (
"fmt"
"time"
)
func main() {
for i := 0; i < 10; i++ {
go show(i)
}
for i := 0; i < 10; i++ {
go func() {
fmt.Printf("go x => %v\n", i)
}()
}
time.Sleep(5 * time.Second)
}
func show(x int) {
fmt.Printf("x => %v\n", x)
}
输出的结果:
[Running] go run "/Users/emacsist/Documents/go/test/src/t1/main.go"
x => 1
x => 3
x => 2
x => 4
go x => 10
x => 7
x => 8
go x => 10
x => 6
go x => 10
go x => 10
x => 0
x => 5
go x => 10
go x => 10
go x => 10
go x => 10
go x => 10
x => 9
go x => 10
切片的 copy 与 赋值区别
package main
import "fmt"
func main() {
copySlice()
fmt.Println("------------")
assignlice()
}
func copySlice() {
hello := []string{"你", "好"}
c := make([]string, 2, 2)
copy(c, hello)
fmt.Printf("%v\n", hello)
fmt.Printf("%v\n", c)
c[0] = "您"
fmt.Printf("%v\n", hello)
fmt.Printf("%v\n", c)
}
func assignlice() {
hello := []string{"你", "好"}
c := hello
fmt.Printf("%v\n", hello)
fmt.Printf("%v\n", c)
c[0] = "您"
fmt.Printf("%v\n", hello)
fmt.Printf("%v\n", c)
}
输出:
[Running] go run "/Users/emacsist/Documents/go/test/src/t1/main.go"
[你 好]
[你 好]
[你 好]
[您 好]
------------
[你 好]
[你 好]
[您 好]
[您 好]
切片、字典与性能
避免循环里使用:
var h []string
for i:=0;i<10;i++ {
h = append(h, "你好")
}
而应该事先分配好足够的内存,再循环里使用:
h := make([]string, 0, 10)
for i:=0;i<10;i++ {
h = append(h, "你好")
}
字典同样。
切片中的使用注意
直接访问来修改,与赋值给一个临时变量,然后再修改这个临时变量要千万注意:直接访问修改是会影响原来的,而修改赋值给临时变量的,则只是反映到临时变量里,而不会修改原值(指针的类型除外)
package main
import (
"fmt"
)
func main() {
var bidList []Bid
bid1 := Bid{ID: "ID", Impid: "impID", Price: 232, NURL: "http://", Adm: "adm", Crid: "crid"}
bid2 := Bid{ID: "ID2", Impid: "impID2", Price: 2322, NURL: "http2://", Adm: "adm2", Crid: "crid2"}
bidList = append(bidList, bid1, bid2)
fmt.Printf("%v\n", bidList)
change(bidList)
fmt.Printf("%v\n", bidList)
change2(bidList)
fmt.Printf("%v\n", bidList)
}
func change(bidList []Bid) {
bid1Copy := bidList[0]
bid1Copy.ID = "IDcopy"
}
func change2(bidList []Bid) {
bidList[0].ID = "IDCopy2"
}
type Bid struct {
//DSP 对该次出价分配的ID
ID string `json:"id"`
//Bid Request 中对应的曝光ID
Impid string `json:"impid"`
//DSP 出价,单位是分/千次曝光,即CPM
Price int32 `json:"price"`
//win notice url
NURL string `json:"nurl"`
//广告物料(博文样式返回mid)
Adm string `json:"adm"`
//广告创意ID
Crid string `json:"crid"`
}
输出的结果为:
[Running] go run "/Users/emacsist/Documents/go/test/src/t1/main.go"
[{ID impID 232 http:// adm crid} {ID2 impID2 2322 http2:// adm2 crid2}]
[{ID impID 232 http:// adm crid} {ID2 impID2 2322 http2:// adm2 crid2}]
[{IDCopy2 impID 232 http:// adm crid} {ID2 impID2 2322 http2:// adm2 crid2}]
如果修改为:
package main
import (
"fmt"
)
func main() {
var bidList []Bid
bid1 := Bid{ID: "ID", Impid: "impID", Price: 232, NURL: "http://", Adm: "adm", Crid: "crid"}
bid2 := Bid{ID: "ID2", Impid: "impID2", Price: 2322, NURL: "http2://", Adm: "adm2", Crid: "crid2"}
bidList = append(bidList, bid1, bid2)
fmt.Printf("%v\n", bidList)
change(bidList)
fmt.Printf("%v\n", bidList)
change2(bidList)
fmt.Printf("%v\n", bidList)
}
func change(bidList []Bid) {
bid1Copy := &bidList[0]
bid1Copy.ID = "IDcopy"
}
func change2(bidList []Bid) {
bidList[0].ID = "IDCopy2"
}
type Bid struct {
//DSP 对该次出价分配的ID
ID string `json:"id"`
//Bid Request 中对应的曝光ID
Impid string `json:"impid"`
//DSP 出价,单位是分/千次曝光,即CPM
Price int32 `json:"price"`
//win notice url
NURL string `json:"nurl"`
//广告物料(博文样式返回mid)
Adm string `json:"adm"`
//广告创意ID
Crid string `json:"crid"`
}
即将 change 里的 bid1Copy := bidList[0]
修改为 bid1Copy := &bidList[0]
,那输出的结果为:
[Running] go run "/Users/emacsist/Documents/go/test/src/t1/main.go"
[{ID impID 232 http:// adm crid} {ID2 impID2 2322 http2:// adm2 crid2}]
[{IDcopy impID 232 http:// adm crid} {ID2 impID2 2322 http2:// adm2 crid2}]
[{IDCopy2 impID 232 http:// adm crid} {ID2 impID2 2322 http2:// adm2 crid2