V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
fumeboy
V2EX  ›  Go 编程语言

一个构造结构体的方法

  •  
  •   fumeboy · 2020-12-23 14:55:53 +08:00 · 1332 次点击
    这是一个创建于 1210 天前的主题,其中的信息可能已经有所发展或是发生改变。

    假设要构造 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()
    	}
    }
    
    7 条回复    2020-12-27 17:24:13 +08:00
    vincentxue
        1
    vincentxue  
       2020-12-24 08:16:42 +08:00   ❤️ 1
    这种写法是受了 OO 思想的影响,最好还是不要用 OO 的思想去写 Go 。
    sxfscool
        2
    sxfscool  
       2020-12-24 08:49:19 +08:00
    有啥用?
    siteshen
        3
    siteshen  
       2020-12-24 09:25:44 +08:00   ❤️ 1
    「构造函数」的本质是「无中生有」,第三种是「有中生有」,不配称作「构造函数」。

    func (s *testStruct) New() *testStruct
    sthwrong
        4
    sthwrong  
       2020-12-24 10:42:28 +08:00   ❤️ 1
    都已经有了&testStruct{}, 为啥还要 New 一个?
    fumeboy
        5
    fumeboy  
    OP
       2020-12-24 11:03:14 +08:00 via Android
    @sthwrong 初始化
    reus
        6
    reus  
       2020-12-26 16:41:39 +08:00 via Android   ❤️ 1
    你这是初始化,并不是分配,所以应该叫 Init,不叫 New
    UnknownDomain
        7
    UnknownDomain  
       2020-12-27 17:24:13 +08:00   ❤️ 1
    可以无限的 New 下去
    (&testStruct{}).New().New().New()
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2961 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 15:19 · PVG 23:19 · LAX 08:19 · JFK 11:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.