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 ...
随机推荐
- GPS坐标系
本次测试之坑,人车定位偏差,分析如下 车的定位由后台提供,由gps上报位置,采用WGS-84坐标系 前端(app/小程序)使用腾讯地图,或者高德地图,采用的是GCJ-02坐标系,或者在GCJ-02基础 ...
- 在html中使用javascript总结
对于初学者运行代码的第一步,首先是怎么把你所写的js代码与html代码之间关联起来,只有关联了,js才能控制html中的代码,进而达到控制页面的目的,我总结了html引用js的方法,一方面可以时时复习 ...
- python基础入门之对文件的操作
**python**文件的操作1.打开文件 打开文件:open(file,mode='r') file:操作文件的路径加文件名 #绝对路径:从根目录开始的 #相对路径:从某个路径开始 mode:操作文 ...
- DNN原理探究系列之目录与序章篇
序言: 神经网络结构,作为最成功的机器学习模型之一,其工作原理一直被埋藏得比较深,其解释性以至于被称为黑盒. 自己对于DNN的理解也只能算刚踏入了门槛,对于人脑的原理与DNN原理之间的互通性,一直是非 ...
- ES部署报错 max file size 和 kibana 报错File size limit exceeded
启动失败一 ERROR: [2] bootstrap checks failed [1]: max file descriptors [4096] for elasticsearch process ...
- 两条比较实用的mysql导入导出命令
开发lamp程序,对mysql数据库的导入导出是经常的事情,我就遇到这个问题,不能很方便的将数据库导入导出.今天整理了两条比较实用的命令,轻松搞定导入导出问题. 首先是导出命令 1.导出数据库 mys ...
- 关于this的指向
1.谁调用该函数this指向就指向谁 2.回调函数中this的指向永远都指向window 3.箭头函数指向最近的作用域,箭头函数本身是没有this的指向 4.定时器永远指向window 5.严格模式下 ...
- selenium批量执行脚本操作
import unittest import os from HTMLTestRunner import HTMLTestRunner # 待执行用例的目录 def allcase(): #引入执行用 ...
- 去掉ACM论文左下角和页眉
在\documentclass下添加如下命令: \fancyhead{} //去掉页眉 \settopmatter{printacmref=false} % Removes citation info ...
- temp--内蒙农信出差
============================2018.09.18~~~20181001================================== -------住宿----黎明花 ...