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

结构体套结构体指针--有什么黑魔法么

  •  
  •   hongyexiaoqing · 2020-07-23 16:56:18 +08:00 · 1783 次点击
    这是一个创建于 1365 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在看 k8s 源代码时候,有这么一段代码,其中 Complete()方法, 看注释是可以填充没有设置的字段,填充字段需要这么套娃么.

    CompletedConfig --》 指针 completedConfig --》 Config 指针

    // Config is the main context object for the controller manager.
    type Config struct {
    	ComponentConfig kubectrlmgrconfig.KubeControllerManagerConfiguration
    
    	SecureServing *apiserver.SecureServingInfo
    	// LoopbackClientConfig is a config for a privileged loopback connection
    	LoopbackClientConfig *restclient.Config
    
    	// TODO: remove deprecated insecure serving
    	InsecureServing *apiserver.DeprecatedInsecureServingInfo
    	Authentication  apiserver.AuthenticationInfo
    	Authorization   apiserver.AuthorizationInfo
    
    	// the general kube client
    	Client *clientset.Clientset
    
    	// the client only used for leader election
    	LeaderElectionClient *clientset.Clientset
    
    	// the rest config for the master
    	Kubeconfig *restclient.Config
    
    	// the event sink
    	EventRecorder record.EventRecorder
    }
    
    type completedConfig struct {
    	*Config
    }
    
    // CompletedConfig same as Config, just to swap private object.
    type CompletedConfig struct {
    	// Embed a private pointer that cannot be instantiated outside of this package.
    	*completedConfig
    }
    
    // Complete fills in any fields not set that are required to have valid data. It's mutating the receiver.
    func (c *Config) Complete() *CompletedConfig {
    	cc := completedConfig{c}
    
    	apiserver.AuthorizeClientBearerToken(c.LoopbackClientConfig, &c.Authentication, &c.Authorization)
    
    	return &CompletedConfig{&cc}
    }
    
    
    7 条回复    2020-07-24 15:38:53 +08:00
    wangritian
        1
    wangritian  
       2020-07-23 17:37:07 +08:00
    其他包可以创建 Config 对象,设置属性,然后通过 Complete 方法补全,得到一个隐藏全部细节的 CompletedConfig,不会有机会再改动属性,看上去是这个目的
    lance6716
        2
    lance6716  
       2020-07-23 18:45:40 +08:00 via Android
    注释不是写的很清楚吗
    yangbonis
        3
    yangbonis  
       2020-07-23 20:13:59 +08:00 via iPhone
    only write once
    yangbonis
        4
    yangbonis  
       2020-07-23 20:25:09 +08:00 via iPhone
    它这个 comment 不如写成,Only allow write once access.
    raaaaaar
        5
    raaaaaar  
       2020-07-23 20:39:51 +08:00 via Android
    手机看代码真难受
    hongyexiaoqing
        6
    hongyexiaoqing  
    OP
       2020-07-24 15:33:03 +08:00
    @wangritian
    恩,谢谢,注释写的模拟量可
    hongyexiaoqing
        7
    hongyexiaoqing  
    OP
       2020-07-24 15:38:53 +08:00
    如果原始的 Config 改变了,感觉防止不了 Config 被改

    比如

    ```
    a := &Config{}

    b:=a.Complete()

    如果 a 在某个地方被改了,b 不是也被改了

    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3231 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 13:00 · PVG 21:00 · LAX 06:00 · JFK 09:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.