zoukankan      html  css  js  c++  java
  • 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类型接收才能还原。
    
  • 相关阅读:
    Sql Server截断日志(转)
    C#/VB.NET语法的比较(转)
    ReportViewer矩阵报表
    逐步学习 iPhone App 开发(1)
    一月二十四日,无家可归
    再见2009
    poj 1376 机器人广搜
    hdu 1004 颜色问题
    hdu 2734 简单地字符串处理
    1116 贪婪的送礼者
  • 原文地址:https://www.cnblogs.com/flhs/p/13635107.html
Copyright © 2011-2022 走看看