<|start_filename|>5.standard-library/copy/my_copy/copy_b_test.go<|end_filename|>
package my_copy

import "testing"

func BenchmarkFoo_Duplicate(b *testing.B) {
	b.StopTimer()
	var a = 1
	var t1 = Foo{IntPtr: &a}
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		_ = t1.Duplicate()
	}
}

func BenchmarkDeepCopyByGob(b *testing.B) {
	b.StopTimer()
	var a = 1
	var t1 = Foo{IntPtr: &a}
	t2 := Foo{}
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		_ = DeepCopyByGob(&t2, t1)
	}
}

func BenchmarkDeepCopyByJson(b *testing.B) {
	b.StopTimer()
	var a = 1
	var t1 = Foo{IntPtr: &a}
	t2 := Foo{}
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		_ = DeepCopyByJson(&t2, t1)
	}
}


<|start_filename|>5.standard-library/copy/my_copy/copy.go<|end_filename|>
package my_copy

import (
	"bytes"
	"encoding/gob"
	"encoding/json"
)

type Foo struct {
	List   []int             `json:"list"`
	FooMap map[string]string `json:"foo_map"`
	IntPtr *int              `json:"int_ptr"`
}

func (f *Foo) Duplicate() Foo {
	var tmp = Foo{
		List:   make([]int, 0, len(f.List)),
		FooMap: make(map[string]string),
		IntPtr: new(int),
	}

	copy(tmp.List, f.List)
	for i := range f.FooMap {
		tmp.FooMap[i] = f.FooMap[i]
	}
	if f.IntPtr != nil {
		*tmp.IntPtr = *f.IntPtr
	} else {
		tmp.IntPtr = nil
	}
	return tmp
}

func DeepCopyByJson(dst, src interface{}) error {
	b, err := json.Marshal(src)
	if err != nil {
		return err
	}
	err = json.Unmarshal(b, dst)

	return err
}
func DeepCopyByGob(dst, src interface{}) error {
	var buffer bytes.Buffer
	if err := gob.NewEncoder(&buffer).Encode(src); err != nil {
		return err
	}

	return gob.NewDecoder(&buffer).Decode(dst)
}

//
//func main() {
//	fmt.Println("手动拷贝方法")
//	var a = 1
//	var t1 = Foo{IntPtr: &a}
//	t2 := t1.Duplicate()
//	a = 2
//	fmt.Println(*t1.IntPtr)
//	fmt.Println(*t2.IntPtr)
//	fmt.Println("json序列化反序列化方法")
//	a = 3
//	t1 = Foo{IntPtr: &a}
//	t2 = Foo{}
//	_ = DeepCopyByJson(&t2, t1)
//	fmt.Println(*t1.IntPtr)
//	fmt.Println(*t2.IntPtr)
//	fmt.Println("gob序列化反序列化方法")
//	a = 4
//	t1 = Foo{IntPtr: &a}
//	t2 = Foo{}
//	_ = DeepCopyByGob(&t2, t1)
//	fmt.Println(*t1.IntPtr)
//	fmt.Println(*t2.IntPtr)
//}


<|start_filename|>5.standard-library/5.4-cmd/5.4.1/5.4.1-flag.go<|end_filename|>
package main

import (
	"flag"
	"fmt"
	"os"
)

var (
	version = "1.0.0"
	showVersion = flag.Bool("version", false, "show version information")
	isDebug = flag.Bool("debug", false, "is debug")
	ip      = flag.String("ip", "127.0.0.1", "Input bind address")
	port    = flag.Int("port", 80, "Input bind port")
)

func main() {
	flag.Parse()
	if *showVersion {
		fmt.Println(version)
		os.Exit(0)
	}
	if *isDebug {
		fmt.Println("set log level: debug")
	}
	fmt.Println(fmt.Sprintf("bind address: %s:%d successfully",*ip,*port))
}


<|start_filename|>5.standard-library/5.4-cmd/5.4.3/5.4.2.go<|end_filename|>
package main

import (
	"fmt"
	"gopkg.in/alecthomas/kingpin.v2"
	"os"
)
var(
	showVersion = kingpin.Flag("version",
		"show version information",
	).Default("false").Bool()
	isDebug = kingpin.Flag("isDebug",
		"is debug mode(default: false)",
	).Default("false").Bool()
	ip = kingpin.Flag("ip",
		"Input bind address(default: 127.0.0.1)",
	).Default("127.0.0.1").String()
	port    = kingpin.Flag("port",
		"Input bind port(default: 80)",
		).Default("80").Int()
)
func main() {
	kingpin.HelpFlag.Short('h')
	kingpin.Parse()
	if *showVersion{
		fmt.Println("1.0.0")
		os.Exit(0)
	}
	if *isDebug {
		fmt.Println("set log level: debug")
	}
	fmt.Println(fmt.Sprintf("bind address: %s:%d successfully",*ip,*port))

}



<|start_filename|>5.standard-library/5.4-cmd/5.4.2/flag_easy.go<|end_filename|>
package main

import (
	"flag"
	"fmt"
)

var (
	ip          string
	port        int
)

func init() {
	flag.StringVar(&ip, "ip", "127.0.0.1", "Input bind address(default: 127.0.0.1)")
	flag.IntVar(&port, "port", 80, "Input bind port(default: 80)")
}
func main() {
	flag.Parse()
	fmt.Println(fmt.Sprintf("bind address: %s:%d successfully", ip, port))
}


<|start_filename|>5.standard-library/append_bug/main.go<|end_filename|>
package main

import "fmt"

func main() {
	var a = make([]int, 0)
	for i := 0; i < 3; i++ {
		a = append(a, i)
	}
	b := append(a, 1)
	c := append(a, 2)
	d := append(a, 3)
	fmt.Println(b[3], c[3], d[3])
}
