前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang面向对象编程之构造函数【struct&new】

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

作者头像
Allen.Wu
修改2022-11-12 01:18:42
6.9K0
修改2022-11-12 01:18:42
举报

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

201808

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

Golang里面没有构造函数,但是Golang却可以像C++一样实现类似继承、构造函数一样等面向对象编程的思想和方法。Golang里面要实现相关的构造函数定义可以通过通过new来创建构造函数。

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

定义一个结构

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

通过new一个对象,或者利用Golang本身的&方式来生成一个对象并返回一个对象指针:

代码语言:javascript
复制
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,
    }
}

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

代码语言:javascript
复制
/*
一个更为优雅的构造函数的实现方式

参考:
* 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)

}

欢迎关注我的公众号,方便更快捷的接收最新优质文章
欢迎关注我的公众号,方便更快捷的接收最新优质文章
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-09-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 后端系统和架构 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Golang面向对象编程之构造函数【struct&new】
    • 一个简单的构造函数的实现
      • 更为优雅的构造的函数的实现
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档