golang实现参数可变的技巧
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实现参数可变的技巧的更多相关文章
- Java可变参数/可变长参数
Java可变参数/可变长参数 传递的参数不确定长度,是变长的参数,例如小例子: package demo; public class Demo { public static int sum(int ...
- Golang fmt包使用小技巧
h1 { margin-top: 0.6cm; margin-bottom: 0.58cm; direction: ltr; color: #000000; line-height: 200%; te ...
- 随想:目标识别中,自适应样本均衡设计,自适应模型结构(参数可变自适应,模型结构自适应,数据类别or分布自适应)
在现在的机器学习中,很多人都在研究自适应的参数,不需要人工调参,但是仅仅是自动调参就不能根本上解决 ai识别准确度达不到实际生产的要求和落地困难的问题吗?结论可想而知.如果不改变参数,那就得从算法的结 ...
- java参数可变方法
java中允许一个方法中存在多个参数 public class Parmvarexmple { //参数可变的方法 public int sum(int...n) { int tempSum=0; f ...
- Golang的防坑小技巧
Golang的防坑小技巧 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 作为一名小白,在之前没有接触到编程的小伙伴,难免会踩到一些坑,比如说刚刚入门的时候你需要安装环境,学习Gol ...
- Python星号*与**用法分析 What does ** (double star/asterisk) and * (star/asterisk) do for parameters? 必选参数 默认参数 可变参数 关键字参数
python中*号**的区别 - CSDN博客 https://blog.csdn.net/qq_26815677/article/details/78091452 定义可变参数和定义 list 或 ...
- golang的超时处理使用技巧
原文链接:https://www.zhoubotong.site/post/57.html golang的超时处理 2天前Go实例技巧25 大家知道Select 是 Go 中的一个控制结构,每个 ...
- SQL Server存储过程Return、output参数及使用技巧
SQL Server目前正日益成为WindowNT操作系统上面最为重要的一种数据库管理系统,随着 SQL Server2000的推出,微软的这种数据库服务系统真正地实现了在WindowsNT/2000 ...
- python函数的参数-可变参数,关键字参数
# -*- coding: utf-8 -*- #coding=utf-8 ''' @author: tomcat @license: (C) Copyright 2017-2019, Persona ...
随机推荐
- arr.sort()
var ary = [12,2,0,15,32,125,52,63,45,24]; /* * sort实现原理 每一次拿出数组中的当前项和后一项,每一次这样的操作都会让传递的匿名函数执行一次,不仅执行 ...
- ORACLE窗口函数
--ORACLE窗口函数,是针对分析用的. --create tablecreate table EMP ( empno NUMBER(4) not null, ename VARCHAR2(10), ...
- Apollo
下载源码: https://github.com/nobodyiam/apollo-build-scripts#%E4%B8%80%E5%87%86%E5%A4%87%E5%B7%A5%E4%BD%9 ...
- c语言实验一
#include <stdio.h> int main(){ int a,b,sum; a=123; b=456; sum = a + b; printf("sum is %d\ ...
- Spring-boot集成RabbitMQ踩过的坑
1.java.net.SocketException: socket closed 官方文档已经说明,新建user和guest的账户是没有远程登录的权限的 需要对登录所用账户授权 解决方法: rabb ...
- MySQL数据库学习书单/书籍
MySQL数据库学习书单 1.MySQL必知必会(MySQL Crash Course) 豆瓣详情 主要适合前期掌握MySQL,豆瓣评分8.4. 2.高性能MySQL 豆瓣详情 主要适合进阶阅读使 ...
- 1111. Online Map (30)
Input our current position and a destination, an online map can recommend several paths. Now your jo ...
- python简单入门
一. 初识python. 1. 认识计算机 CPU(大脑) 3GHZ + 内存(DDR4) + 主板 + 电源(心脏)+ 显示器 + 键盘 +鼠标+ 显卡 + 硬盘 80MB/s 操作系统 windo ...
- vue中引入vux
1.安装相关依赖 cnpm install vux --save cnpm install vux-loader --save-dev cnpm install less less-loader -- ...
- 隐藏Spring Elements
Spring Elements 显示出来很烦,如何隐藏