[TOC]html

Golang面向对象编程之构造函数【struct&new】

201808git

构造函数是一种特殊的方法,主要用来在建立对象时初始化对象,即为对象成员变量赋初始值。特别的一个类能够有多个构造函数 ,可根据其参数个数的不一样或参数类型的不一样来区分它们,即构造函数的重载。golang

Golang里面没有构造函数,可是Golang却能够像C++同样实现相似继承、构造函数同样等面向对象编程的思想和方法。Golang里面要实现相关的构造函数定义能够经过经过new来建立构造函数。redis

一个简单的构造函数的实现

定义一个结构编程

type ContentMsg struct {
	EffectId int         `json:"effect_id"`
	Text     string      `json:"text"`
	Data     interface{} `json: "data"`
}
复制代码

经过new一个对象,或者利用Golang自己的&方式来生成一个对象并返回一个对象指针:json

unc NewContentMsg(data, effectId int) *ContentMsg {
	instance := new(ContentMsg)
	instance.Data = data
	instance.EffectId = effectId
	return instance
}

func NewContentMsgV2(data, effectId int) *ContentMsg {
	return &ContentMsg{
		Data:     data,
		EffectId: effectId,
	}
}

复制代码

更为优雅的构造的函数的实现

/*
一个更为优雅的构造函数的实现方式

参考:
* 1,项目:"gitlab.xxx.com/xxx/redis"
* 2,连接:https://commandcenter.blogspot.com/2014/01/self-referential-functions-and-design.html

经过这个方式能够方便构造不一样对象,同时避免了大量重复代码

*/

package main

import (
	"fmt"
	"time"

	"golang.org/x/net/context"
)

type Cluster struct {
	opts options
}

type options struct {
	connectionTimeout time.Duration
	readTimeout       time.Duration
	writeTimeout      time.Duration
	logError          func(ctx context.Context, err error)
}

// 经过一个选项实现为一个函数指针来达到一个目的:设置选项中的数据的状态
// Golang函数指针的用法
type Option func(c *options)

// 设置某个参数的一个具体实现,用到了闭包的用法。
// 不单单只是设置而采用闭包的目的是为了更为优化,更好用,对用户更友好
func LogError(f func(ctx context.Context, err error)) Option {
	return func(opts *options) {
		opts.logError = f
	}
}

func ConnectionTimeout(d time.Duration) Option {
	return func(opts *options) {
		opts.connectionTimeout = d
	}
}

func WriteTimeout(d time.Duration) Option {
	return func(opts *options) {
		opts.writeTimeout = d
	}
}

func ReadTimeout(d time.Duration) Option {
	return func(opts *options) {
		opts.readTimeout = d
	}
}

// 构造函数具体实现,传入相关Option,new一个对象并赋值
// 若是参数不少,也不须要传入不少参数,只须要传入opts ...Option便可
func NewCluster(opts ...Option) *Cluster {
	clusterOpts := options{}
	for _, opt := range opts {
		// 函数指针的赋值调用
		opt(&clusterOpts)
	}

	cluster := new(Cluster)
	cluster.opts = clusterOpts

	return cluster
}

func main() {

	// 前期储备,设定相关参数
	commonsOpts := []Option{
		ConnectionTimeout(1 * time.Second),
		ReadTimeout(2 * time.Second),
		WriteTimeout(3 * time.Second),
		LogError(func(ctx context.Context, err error) {
		}),
	}

	// 终极操做,构造函数
	cluster := NewCluster(commonsOpts...)

	// 测试验证
	fmt.Println(cluster.opts.connectionTimeout)
	fmt.Println(cluster.opts.writeTimeout)

}


复制代码

【"欢迎关注个人微信公众号:Linux 服务端系统研发,后面会大力经过微信公众号发送优质文章"】bash

个人微信公众号