tiup 源码的问题

请问下各位老师,最近在看tiup 源码
src/tiup-1.11.0/pkg/cluster/manager/manager.go 里面有这么一行
metadata = m.specManager.NewMetadata()
https://github.com/pingcap/tiup/blob/master/pkg/cluster/manager/manager.go#:~:text=metadata%20%3D%20m.specManager.NewMetadata()

这里其实调用m.specManager.NewMetadata() 后返回的是1个接口(这里还不是很懂为什么这么写)


但是我自己模拟这部分代码写的时候,发现报错,烦请各位老师看下,哪里写错了

type Metadata interface {
	GetTopology() Topology
}

type Topology interface {
	Type() string
}

func (s *Specification) Type() string {
	TopoTypeTiDB := "tidb-cluster"
	return TopoTypeTiDB
	//TopoTypeTiDB      = "tidb-cluster"
}

type Specification struct {
	GlobalOptions GlobalOptions `yaml:"global,omitempty" validate:"global:editable"`
	//MonitoredOptions MonitoredOptions     `yaml:"monitored,omitempty" validate:"monitored:editable"`
	//ServerConfigs    ServerConfigs        `yaml:"server_configs,omitempty" validate:"server_configs:ignore"`
	//TiDBServers      []*TiDBSpec          `yaml:"tidb_servers"`
	//TiKVServers      []*TiKVSpec          `yaml:"tikv_servers"`
	//TiFlashServers   []*TiFlashSpec       `yaml:"tiflash_servers"`
	//PDServers        []*PDSpec            `yaml:"pd_servers"`
	//DashboardServers []*DashboardSpec     `yaml:"tidb_dashboard_servers,omitempty"`
	//PumpServers      []*PumpSpec          `yaml:"pump_servers,omitempty"`
	//Drainers         []*DrainerSpec       `yaml:"drainer_servers,omitempty"`
	//CDCServers       []*CDCSpec           `yaml:"cdc_servers,omitempty"`
	//TiKVCDCServers   []*TiKVCDCSpec       `yaml:"kvcdc_servers,omitempty"`
	//TiSparkMasters   []*TiSparkMasterSpec `yaml:"tispark_masters,omitempty"`
	//TiSparkWorkers   []*TiSparkWorkerSpec `yaml:"tispark_workers,omitempty"`
	//Monitors         []*PrometheusSpec    `yaml:"monitoring_servers"`
	//Grafanas         []*GrafanaSpec       `yaml:"grafana_servers,omitempty"`
	//Alertmanagers    []*AlertmanagerSpec  `yaml:"alertmanager_servers,omitempty"`
}

type GlobalOptions struct {
	User    string `yaml:"user,omitempty" default:"tidb"`
	Group   string `yaml:"group,omitempty"`
	SSHPort int    `yaml:"ssh_port,omitempty" default:"22" validate:"ssh_port:editable"`
	//SSHType         executor.SSHType     `yaml:"ssh_type,omitempty" default:"builtin"`
	TLSEnabled bool   `yaml:"enable_tls,omitempty"`
	DeployDir  string `yaml:"deploy_dir,omitempty" default:"deploy"`
	DataDir    string `yaml:"data_dir,omitempty" default:"data"`
	LogDir     string `yaml:"log_dir,omitempty"`
	//ResourceControl meta.ResourceControl `yaml:"resource_control,omitempty" validate:"resource_control:editable"`
	OS     string      `yaml:"os,omitempty" default:"linux"`
	Arch   string      `yaml:"arch,omitempty"`
	Custom interface{} `yaml:"custom,omitempty" validate:"custom:ignore"`
}

type Manager struct {
	sysName     string
	specManager *SpecManager
	//bindVersion spec.BindVersion
	//logger      *logprinter.Logger
}

type SpecManager struct {
	base    string
	newMeta func() Metadata
}

func (s *SpecManager) NewMetadata() Metadata {
	return s.newMeta()
}

func (m *Manager) meta(name string) {

	metadata := m.specManager.NewMetadata()
	fmt.Println(metadata)

}

func main() {
	var cm *Manager
	cm.meta("tidb-test")
}

报错如下所示

你的cm是个nil,所以报了空指针引用异常。
要先创建一个Manager实例。

那为啥这样执行就可以了

问题出在你注释掉的第一行,m是个nil值,不能使用m.xxx引用它的成员变量

懂了,谢谢大佬

此话题已在最后回复的 60 天后被自动关闭。不再允许新回复。