`

go singleton

    博客分类:
  • go
 
阅读更多
package main

import "fmt"

type person struct {
	Name   string
	age    int32
	Height int32
}

func getPerson() *person {
	return &person{
		Name:   "zhangesan",
		age:    18,
		Height: 170,
	}
}

func (p1 *person) getAge() int32 {
	return p1.age
}

//每个源码可以使用 1 个 init() 函数。
//init() 函数会在程序执行前(main() 函数执行前)被自动调用。
//init() 函数不能被其他函数调用。
func init() {
	fmt.Println("init")
}

func main() {
	p := getPerson()
	fmt.Println(p.Name, p.getAge(), p.Height)
}


zhangesan 18 170
分享到:
评论

相关推荐

    go语言单例模式(Singleton)实例分析

    本文实例讲述了go语言单例模式(Singleton)用法。分享给大家供大家参考。具体分析如下: 单例模式(Singleton):表示一个类只会生成唯一的一个对象。单例模式具有如下性质: A.这些类只能有一个实例; B.这些能够...

    Packt.Go.Design.Patterns.2017

    Creational Patterns - Singleton, Builder, Factory, Prototype, and Abstract Factory Design Patterns Chapter 3. Structural Patterns - Composite, Adapter, and Bridge Design Patterns Chapter 4. ...

    有用模式的集合-Golang开发

    要求golang> = 1.11.x-Go编程语言安装程序go get github.com/donutloop/toolkit/{any_package}示例go get github.com/donutloop/toolkit/worker模式Worker作业时间表Singleton重试Promise Multierror循环租赁事件-...

    gospring:受Java Spring Framework启发的Go注入库

    受Java Spring Framework启发的Go注入库。 Java Spring框架是著名的IoC框架。 这个库试图模仿spring的功能。 快速开始 定义您的执行结构 type Astruct struct { // native type IntValue int StringValue ...

    二十三种设计模式【PDF版】

    设计模式之 Singleton(单态/单件) 阎宏博士讲解:单例(Singleton)模式 保证一个类只有一个实例,并提供一个访问它的全局访问点 设计模式之 Factory(工厂方法和抽象工厂) 使用工厂模式就象使用 new 一样频繁. ...

    Design.Patterns.Explained.Simply

    When you finish reading this book, you'll have good reason to go to your boss and ask him for apromotion. Why? Because using design patterns will allow you to get your tasks done twice as fast, to ...

    JavaScript Patterns

    * Study sample JavaScript approaches to common design patterns such as Singleton, Factory, Decorator, and more * Examine patterns that apply specifically to the client-side browser environment

    AtSugar:您的@的糖

    糖 AtSugar为您的@符号提供了一些语法糖。 要获得更多背景知识并更深入地了解AtSugar,...// Easily implement the singleton pattern // @interface MyClass : NSObject + ( instancetype ) sharedInstance ; @end @i

    设计模式2:APS_FINAL

    Análisee Desenvolvimento de Sistemas-设计模式 教授:菲利普·盖伯(Filipe Guelber) Aluno: :Gabriel Correia da Silva ...Singleton = Usando para monitorar asposiçãodos jogadores no go on on ele e or

    C# Game Programming Cookbook for Unity 3D - 2014

    1.1.3 Using the Singleton Pattern in Unity...........................5 1.1.4 Inheritance.................................................6 1.1.5 Where to Now?.............................................

    java简易版开心农场源码-GOF23:一起来学习设计模式吧~

    Singleton 的核心思想: 保证一个类只有一个实例, 并且提供一个访问该实例的全局访问点 常见应用场景: Windows 的任务管理器, 回收站 单例模式的优点: 由于单例模式只生成一个实例, 减少了系统性能的开销 当一个对象...

    MySQL server has gone away错误提示解决方法

    还有一种可能是因为某些原因导致超时,比如说程序中获取数据库连接时采用了Singleton的做法,虽然多次连接数据库,但其实使用的都是同一个连接,而且程序中某两次操作数据库的间隔时间超过了wait_timeout(SHOW ...

    工具包:有用模式的集合

    > = 1.11.x-Go编程语言 安装 go get github.com/donutloop/toolkit/{any_package} 例 go get github.com/donutloop/toolkit/worker 模式 例子 每个子目录中都有一组示例 代码生成 Currently, it's only compatiable...

    gof:GoF的23种设计模式

    GoF 设计模式GoF所提出的23种...单例(Singleton)模式:某个类只能生成一个实例,该类提供了一个全局访问点供外部获取该实例,其拓展是有限多例模式。原型(Prototype)模式:将一个对象作为原型,通过对其进行复制

    WebSocket-Chatroom:使用gorilla,nhooyr.io包实作WebSocket聊天室

    WebSocket-聊天室 WebSocket应用练习,聊天室实作 描述 WebSocket包应用(gorilla / nhooyr.io) 利用渠道管理使用者进入与离线状态 使用Singleton的Broadcaster实例统一处理频道 利用goroutine发送消息 演示版

    微软内部资料-SQL性能优化3

    GO Multigranular Locking Multigranular Locking In our example, if one transaction (T1) holds an exclusive lock at the table level, and another transaction (T2) holds an exclusive lock at the ...

    asp.net知识库

    C#2.0 Singleton 的实现 .Net Framwork 强类型设计实践 通过反射调用類的方法,屬性,字段,索引器(2種方法) ASP.NET: State Server Gems 完整的动态加载/卸载程序集的解决方案 从NUnit中理解.NET自定义属性的应用...

    iOS-2048-master

    it does not send you push notifications to ask you to go back and play it. You may also consider to [donate](https://github.com/gabrielecirulli/2048) to the maker of the original 2048 if you'd like,...

    netWindows_0.3.0_pre2

    to optimize NW inline constructor performance (espically on large pages) * singleton objects now declared using var __obj__ = new Object(); syntax instead of creating classes for each. Cuts down on ...

Global site tag (gtag.js) - Google Analytics