Go 使用默认参数的技巧

Functional Options Pattern in Go

golang中没有函数默认参数的设计,因此需要些特别的技巧来实现。

假如我们需要订购一批电脑,其中电脑配置cpu,gpu,内存支持自定义。

type Computer interface {
PowerOn() error
PowerOff() error
} type computer struct {
name string
cpu string
gpu string
memorySize int
}

定义自定义参数配置,创建一个默认的电脑配置

type pcConfigure struct {
cpu string
gpu string
memorySize int
} var DefaultPConfigure = pcConfigure {
"i3 2120",
"GTX 660",
12}

创建一个闭包函数,接受自定义的配置参数,返回一个自定义的参数配置

type newPcConfigure func(*pcConfigure)

func newCpuConfigure(cpu string) newPcConfigure {
return func(o *pcConfigure) {
o.cpu = cpu
}
} func newGpuConfigure(gpu string) newPcConfigure {
return func(o *pcConfigure) {
o.gpu = gpu
}
} func newMemorySizeConfigure(memorySize int) newPcConfigure {
return func(o *pcConfigure) {
o.memorySize = memorySize
}
}

最后一步,定义一个构造coputer的函数,接受上面的闭包函数为参数

func newComputer(name string, opts ...newPcConfigure) *computer {
pcConfigure := DefaultPConfigure
for _, o := range opts {
o(&pcConfigure)
}
return &computer {
name: name,
cpu: pcConfigure.cpu,
gpu: pcConfigure.gpu,
memorySize: pcConfigure.memorySize}
}
完整代码

package main

import (
"fmt"
) type pcConfigure struct {
cpu string
gpu string
memorySize int
} var DefaultPConfigure = pcConfigure {
"i3 2120",
"GTX 660",
12} type newPcConfigure func(*pcConfigure) func newCpuConfigure(cpu string) newPcConfigure {
return func(o *pcConfigure) {
o.cpu = cpu
}
} func newGpuConfigure(gpu string) newPcConfigure {
return func(o *pcConfigure) {
o.gpu = gpu
}
} func newMemorySizeConfigure(memorySize int) newPcConfigure {
return func(o *pcConfigure) {
o.memorySize = memorySize
}
} type Computer interface {
PowerOn() error
PowerOff() error
} type computer struct {
name string
cpu string
gpu string
memorySize int
} func newComputer(name string, opts ...newPcConfigure) *computer {
pcConfigure := DefaultPConfigure
for _, o := range opts {
o(&pcConfigure)
}
return &computer {
name: name,
cpu: pcConfigure.cpu,
gpu: pcConfigure.gpu,
memorySize: pcConfigure.memorySize}
} func (c *computer) PowerOn() (err error) {
return
} func (c *computer) PowerOff() (err error) {
return
} func main () {
pc1 := newComputer("pc1")
fmt.Println(pc1)
pc2 := newComputer("pc2", newCpuConfigure("i7 8700k"), newGpuConfigure("RTX 2080ti"))
fmt.Println(pc2)
}

golang实现参数可变的技巧的更多相关文章

  1. Java可变参数/可变长参数

    Java可变参数/可变长参数 传递的参数不确定长度,是变长的参数,例如小例子: package demo; public class Demo { public static int sum(int ...

  2. Golang fmt包使用小技巧

    h1 { margin-top: 0.6cm; margin-bottom: 0.58cm; direction: ltr; color: #000000; line-height: 200%; te ...

  3. 随想:目标识别中,自适应样本均衡设计,自适应模型结构(参数可变自适应,模型结构自适应,数据类别or分布自适应)

    在现在的机器学习中,很多人都在研究自适应的参数,不需要人工调参,但是仅仅是自动调参就不能根本上解决 ai识别准确度达不到实际生产的要求和落地困难的问题吗?结论可想而知.如果不改变参数,那就得从算法的结 ...

  4. java参数可变方法

    java中允许一个方法中存在多个参数 public class Parmvarexmple { //参数可变的方法 public int sum(int...n) { int tempSum=0; f ...

  5. Golang的防坑小技巧

    Golang的防坑小技巧 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 作为一名小白,在之前没有接触到编程的小伙伴,难免会踩到一些坑,比如说刚刚入门的时候你需要安装环境,学习Gol ...

  6. Python星号*与**用法分析 What does ** (double star/asterisk) and * (star/asterisk) do for parameters? 必选参数 默认参数 可变参数 关键字参数

    python中*号**的区别 - CSDN博客 https://blog.csdn.net/qq_26815677/article/details/78091452 定义可变参数和定义 list 或 ...

  7. golang的超时处理使用技巧

    原文链接:https://www.zhoubotong.site/post/57.html golang的超时处理 2天前Go实例技巧25   大家知道Select 是 Go 中的一个控制结构,每个  ...

  8. SQL Server存储过程Return、output参数及使用技巧

    SQL Server目前正日益成为WindowNT操作系统上面最为重要的一种数据库管理系统,随着 SQL Server2000的推出,微软的这种数据库服务系统真正地实现了在WindowsNT/2000 ...

  9. python函数的参数-可变参数,关键字参数

    # -*- coding: utf-8 -*- #coding=utf-8 ''' @author: tomcat @license: (C) Copyright 2017-2019, Persona ...

随机推荐

  1. OO第二单元单元总结

    总述 OO的第二单元主题是电梯调度,与第一单元注重对数据的输入输出的处理.性能的优化不同,第二单元的重心更多的是在线程安全与线程通信上.这此次单元实验之前,我并未对线程有过了解,更谈不上“使用经验”, ...

  2. c语言01次作业--分支,顺序结构

    C语言--第01次作业 1.1思维导图 1.2本章学习体会及代码量学习体会 1.2.1学习体会 本章学习让我体会良多.首先,不得不承认自己是一个非常马虎的人.常见的问题就是输出格式上常因为没有与题目要 ...

  3. 20175316盛茂淞 2018-2019-2 《Java程序设计》第7周学习总结

    20175316盛茂淞 2018-2019-2 <Java程序设计>第7周学习总结 教材学习内容总结 第八章 常用实用类 一.String类 String类在java.lang包中,jav ...

  4. tomcat与iis公用80端口(已经发布.net项目现在开发Java项目时tomcat在eclipse中localhost:8080打不开问题)

    在开发过.net项目的电脑上安装eclipse配置tomcat运行时打不开页面问题描述,这也是本人亲生经历,找了好多资料网上大多都是tomcat配置问题描述,今天突然想到是不是IIS的问题,果然上网一 ...

  5. php hash_file

    string hash_file ( string $algo , string $filename [, bool $raw_output = FALSE ] ) 参数¶ algo 要使用的哈希算法 ...

  6. 日期类时间类,日期时间类,单例模式,装箱与拆箱,数字类随机数,BigDecimal总结

    1.日期类,时间类,日期时间类 初步日期使用方法及格式转换方法(旧方法): 格式://Mon Jul 30 11:26:05 CST 2018             年月日时分秒    CST代表北 ...

  7. POJ1964-City Game

    给你N×M大的矩阵,里面分别有字符‘F'和’R',要找到一个最大的只有‘F'的矩阵,不能包含有’R‘.N,M<=1000. 一开始的思路是单调栈来求最大矩形面积,因为没看清题目不能包含’R'字符 ...

  8. qscoj 喵哈哈村的打印机游戏 区间dp

    点这里去看题 区间dp ,dp[l][r][d]代表从l到r的区间底色为d,具体看代码 第一次见到区间dp...两个小时对着敲了五遍终于自己敲懂了一遍ac #include<bits/stdc+ ...

  9. C++ 虚函数的两个例子

    1. 第一个例子是朋友告诉我Qt中的某个实现 1 #include <iostream> 2 3 // Qt中的某个实现 4 class A{ 5 public: 6 A() = defa ...

  10. post推送消息时的乱码

    URL realUrl = new URL(url); // 打开和URL之间的连接 URLConnection conn = realUrl.openConnection(); // 设置通用的请求 ...