This repository has been archived on 2021-09-07. You can view files and clone it, but cannot push or open issues or pull requests.
golang-design-patterns/singleton/starving-loading.go
2018-09-27 18:09:18 +08:00

32 lines
501 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import "fmt"
/*
* 饿汉模式
* 在类加载时就初始化对象
*/
// 构建一个结构体,用来实例化单例
type example2 struct {
name string
}
// 声明一个私有变量,作为单例
var instance2 *example2
// init函数将在包初始化时执行实例化单例
func init() {
instance2 = new(example2)
instance2.name = "初始化单例模式"
}
func GetInstance2() *example2 {
return instance2
}
func main() {
s := GetInstance2()
fmt.Println(s.name)
}