假设要构造 testStruct 这个结构体, 常规方法是写一个 func NewStruct() testStruct
函数
这里的方法是 将这个函数 改写成结构体方法 func (s *testStruct) New() *testStruct
比较一下使用时的不同:
_ = NewStruct()
_ = (&testStruct{}).New()
可以发现 New 这个函数被约束到了 testStruct 命名空间下, 这也是我的主要目的
基准测试中, 两种方式的执行效率几乎一致,当 struct 很大时, (&testStruct{}).New()
略有优势
package main
import (
"testing"
)
type testStruct struct {
value int
}
// 三种构造结构体的方法
func NewStruct() *testStruct { // 逃逸
return &testStruct{}
}
func NewStruct2() testStruct { // 不逃逸 但传值时发生拷贝
return testStruct{}
}
func (s *testStruct) New() *testStruct {
// 不逃逸,只拷贝了指针
// 更重要的是收束到了 testStruct 这个命名空间里
return s
}
func TestNew(t *testing.T){
_ = (&testStruct{}).New()
//fmt.Println(s)
}
func BenchmarkNew(b *testing.B){
for i := 0;i<b.N;i++{
_ = (&testStruct{}).New()
}
}
func BenchmarkNew2(b *testing.B){
for i := 0;i<b.N;i++{
_ = NewStruct()
}
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.