定义 保证一个类,只有一个实例存在,同时提供能够全局对该实例访问的方法。 适用场景 系统只需要一个实例对象。 举个栗子 微服务中的数据库访问接口 主函数中执行初始化数据库连接的接口 func main() { InitMysql() } 2.数据库接口的代码实现 var mysqlConnector func InitMysql() { mysqlConnector = initMysql() } // 业务代码 type Shop struct { ID int count int } func SaveData(s *Shop) { mysqlConnector.Create(s) }