conn, err = ln.Accept()
go handleConnection(conn)
看到这里我曾经有个疑问,为什么不是 handleConnection(&conn) ? 会想这个问题的人肯定都知道如果conn是个struct,那就会发生复制,可能是写时复制,也可能是立即复制,这里面发生了什么不太清楚,反正感觉就是浪费了资源,但如果是指针传递,那就放心了,因为我们知道那肯定就是只复制了个指针而已,为了研究清楚这个问题,我做了下实现
如果一个函数的参数是接口类型,传进去的参数可以是指针,也可以不是指针,这得看你传的对象是怎么实现这个接口类型的
如果实现接口的方法的接收器是指针类型,则传参给接口类型时必需是指针,如果不是,则随便传
const width, height = 256, 256
img := image.NewRGBA(image.Rect(0, 0, width, height))
img.Set(0, 0, &color.RGBA{})
img.Set(0, 0, color.RGBA{})
package main
import (
"fmt"
"reflect"
)
type ifa interface {
foo()
}
type test struct {
V int
}
// 如果接收器不是指针,则ifa接口可以是指针,也可以是对象,否则只能是指针
func (t test) foo() {
fmt.Println("fooooo:", t.V)
}
func main() {
var aa ifa = &test{1234}
var ap = &aa
var o = (*ap).(*test)
var o2 = (*ap).(*test)
fmt.Println("main o.v:", o.V)
o2.V = 12
fmt.Println("main o.v:", o.V)
fmt.Printf("main %p \n", &o.V)
foo(ap)
fmt.Println("main end o.v:", o.V)
aa.foo()
}
func foo(f *ifa) {
fmt.Printf("f: type=%v, kind=%v \n", reflect.TypeOf(f), reflect.TypeOf(f).Kind())
if o, ok := (*f).(*test); ok {
fmt.Printf("%p \n", &o.V)
fmt.Println("foo before o.v:", o.V)
o.V = 11
fmt.Println("foo after o.v:", o.V)
}
(*f).foo()
}
回到开始的问题,为什么不是 handleConnection(&conn) ?
1. 系统库本身就是传的指针,没必要
2. &conn取的是conn变量的地址,有被修改的可能,看下面代码
package main
import (
"fmt"
"reflect"
"time"
)
type ifa interface {
foo()
}
type test struct {
V int
}
// 如果接收器不是指针,则ifa接口可以是指针,也可以是对象,否则只能是指针
func (t test) foo() {
fmt.Println("fooooo:", t.V)
}
func main() {
var o = test{1234}
var aa ifa = &o
fmt.Println("main1:", o)
go foo(&aa)
aa = &test{222}
<-time.After(1 * time.Second)
fmt.Println("main2:", o)
}
func foo(f *ifa) {
<-time.After(10 * time.Millisecond)
fmt.Printf("f: type=%v, kind=%v \n", reflect.TypeOf(f), reflect.TypeOf(f).Kind())
if o, ok := (*f).(*test); ok {
fmt.Printf("%p \n", &o.V)
fmt.Println("foo before o.v:", o.V)
o.V = 11
fmt.Println("foo after o.v:", o.V)
}
(*f).foo()
}
能用指针就尽量用指针吧