golang的一些零散笔记

clone struct

待深入研究

func (log *Logger) clone() *Logger {
	copy := *log
	return &copy
}

函数选项模式

接受结构体的某个参数,返回一个选项方法


const (
	defaultPieceLength uint64 = 524288
	defaultTracker = "http://demo.abc.com/"
)

type Meta struct {
	Tracker     string    `json:"tracker"`
	Name        string    `json:"name"`
	Length      uint64    `json:"length"`
	PieceLength uint64    `json:"pieceLength"`
}

type Option func(m *Meta)

func New(file string, options ...Option) *Meta {
	meta := &Meta{
		Tracker:     defaultTracker,
		PieceLength: defaultPieceLength,
	}
	for _, option := range options {
		option(meta)
	}
	return meta
}

func WithName(name string) Option {
	return func(m *Meta) {
		m.Name = name
	}
}

func WithPieceLength(pieceLength uint64) Option {
	return func(m *Meta) {
		m.PieceLength = pieceLength
	}
}

func WithTracker(tracker string) Option {
	return func(m *Meta) {
		m.Tracker = tracker
	}
}

json.Marshal []byte

golang json Marshal默认对[]byte类型进行base64编码处理,Unmarshal时也只能用[]byte类型接收才能还原。

golang.org代理

go env -w GOPROXY=https://goproxy.cn,direct
posted @ 2020-09-08 20:28  虾敏四把刀  阅读(241)  评论(0编辑  收藏  举报