diff --git a/flag/flag_type.go b/flag/flag_type.go new file mode 100644 index 0000000..103f491 --- /dev/null +++ b/flag/flag_type.go @@ -0,0 +1,22 @@ +package main + +import ( + "flag" + "fmt" + "time" +) + +// flag获取命令行参数 +func main() { + //创建一个标志位参数 + name := flag.String("name", "wangao", "define name") + age := flag.Int("age", 9000, "define age") + married := flag.Bool("married", false, "define married") + mTime := flag.Duration("mtime", time.Second, "define mTime") //命令行接受的参数需要带单位,例如1000h:main.exe -mtime 1000h + //使用flag + flag.Parse() + fmt.Println(*name, *age, *married, mTime) + fmt.Println(flag.Args()) //返回命令行参数后的其他参数,以[]string类型返回 + fmt.Println(flag.NArg()) // 返回命令行的其他参数个数 + fmt.Println(flag.NFlag()) //返回使用的命令行参数个数 +} diff --git a/flag/flag_typevar.go b/flag/flag_typevar.go new file mode 100644 index 0000000..5b3172b --- /dev/null +++ b/flag/flag_typevar.go @@ -0,0 +1,14 @@ +package main + +import ( + "flag" + "fmt" +) + +func main() { + var name string + flag.StringVar(&name, "name", "wangao", "define name") + flag.Parse() + fmt.Println(name) + +} diff --git a/flag/os.args.go b/flag/os.args.go new file mode 100644 index 0000000..943d9f7 --- /dev/null +++ b/flag/os.args.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" + "os" +) + +// os.Args 获取命令行参数 +func main() { + fmt.Printf("%#v", os.Args) //获取当前的执行文件和参数 +} diff --git a/networkProgram/TCP/packet_splicing_client.go b/networkProgram/TCP/packet_splicing_client.go index ce6dad1..374e011 100644 --- a/networkProgram/TCP/packet_splicing_client.go +++ b/networkProgram/TCP/packet_splicing_client.go @@ -7,7 +7,7 @@ import ( "course/networkProgram/protocol" ) -// socket_stick/client/main.go +// socket_stick/client/os.args.go func main() { conn, err := net.Dial("tcp", "127.0.0.1:30000") diff --git a/networkProgram/TCP/packet_splicing_server.go b/networkProgram/TCP/packet_splicing_server.go index 54656fc..ce9cff5 100644 --- a/networkProgram/TCP/packet_splicing_server.go +++ b/networkProgram/TCP/packet_splicing_server.go @@ -8,7 +8,7 @@ import ( "net" ) -// socket_stick/server/main.go +// socket_stick/server/os.args.go func proc(conn net.Conn) { defer conn.Close() reader := bufio.NewReader(conn) diff --git a/pprof/pprof.go b/pprof/pprof.go new file mode 100644 index 0000000..91eae0c --- /dev/null +++ b/pprof/pprof.go @@ -0,0 +1,55 @@ +// runtime_pprof/os.args.go +package main + +import ( + "flag" + "fmt" + "os" + "runtime/pprof" + "time" +) + +// 一段有问题的代码 +func logicCode() { + var c chan int //nil 未初始化 + for { + select { + case v := <-c: //阻塞 + fmt.Printf("recv from chan, value:%v\n", v) + default: + + } + } +} + +func main() { + var isCPUPprof bool + var isMemPprof bool + + flag.BoolVar(&isCPUPprof, "cpu", false, "turn cpu pprof on") + flag.BoolVar(&isMemPprof, "mem", false, "turn mem pprof on") + flag.Parse() + + if isCPUPprof { + file, err := os.Create("./cpu.pprof") + if err != nil { + fmt.Printf("create cpu pprof failed, err:%v\n", err) + return + } + pprof.StartCPUProfile(file) + defer pprof.StopCPUProfile() + } + for i := 0; i < 8; i++ { + go logicCode() + } + time.Sleep(20 * time.Second) + if isMemPprof { + file, err := os.Create("./mem.pprof") + if err != nil { + fmt.Printf("create mem pprof failed, err:%v\n", err) + return + } + pprof.WriteHeapProfile(file) + file.Close() + } +} diff --git a/test/group_spilt_string_test.go b/test/group_spilt_string_test.go new file mode 100644 index 0000000..0dea5a2 --- /dev/null +++ b/test/group_spilt_string_test.go @@ -0,0 +1,27 @@ +package Splitstring + +import ( + "reflect" + "testing" +) + +//测试组 + +func TestGroupSplit(t *testing.T) { + type testCase struct { + str string + sep string + want []string + } + testGroup := []testCase{ + {str: "babcbef", sep: "b", want: []string{"", "a", "c", "ef"}}, + {str: "a:b:c", sep: ":", want: []string{"a", "b", "c"}}, + {str: "abcef", sep: "bc", want: []string{"a", "ef"}}, + {str: "沙河有沙又有河", sep: "有", want: []string{"沙河", "沙又", "河"}}, + } + for i, testcase := range testGroup { + if !reflect.DeepEqual(Split(testcase.str, testcase.sep), testcase.want) { + t.Errorf("testcase %d failed,want: %v got: %v", i+1, testcase.want, Split(testcase.str, testcase.sep)) + } + } +} diff --git a/test/groupchild_spilt_string_test.go b/test/groupchild_spilt_string_test.go new file mode 100644 index 0000000..c71df2a --- /dev/null +++ b/test/groupchild_spilt_string_test.go @@ -0,0 +1,30 @@ +package Splitstring + +import ( + "reflect" + "testing" +) + +//子测试 + +func TestGroupChildSplit(t *testing.T) { + type testCase struct { + str string + sep string + want []string + } + testGroup := map[string]testCase{ + "case1": {str: "babcbef", sep: "b", want: []string{"", "a", "c", "ef"}}, + "case2": {str: "a:b:c", sep: ":", want: []string{"a", "b", "c"}}, + "case3": {str: "abcef", sep: "bc", want: []string{"a", "ef"}}, + "case4": {str: "沙河有沙又有河", sep: "有", want: []string{"沙河", "沙又", "河"}}, + } + for name, testcase := range testGroup { + t.Run(name, func(t *testing.T) { + got := Split(testcase.str, testcase.sep) + if !reflect.DeepEqual(got, testcase.want) { + t.Errorf("got:%v, want:%v", got, testcase.want) + } + }) + } +} diff --git a/test/spilt_string.go b/test/spilt_string.go index e6acb35..45748b5 100644 --- a/test/spilt_string.go +++ b/test/spilt_string.go @@ -1,6 +1,8 @@ package Splitstring -import "strings" +import ( + "strings" +) //切割字符串 //example @@ -21,11 +23,11 @@ import "strings" // abc,b =>[a,c] func Split(str string, sep string) []string { - ret := make([]string, 0) + var ret = make([]string, 0, strings.Count(str, sep)+1) index := strings.Index(str, sep) for index >= 0 { ret = append(ret, str[:index]) - str = str[index+1:] + str = str[index+len(sep):] index = strings.Index(str, sep) } ret = append(ret, str) diff --git a/test/spilt_string_test.go b/test/spilt_string_test.go index 98947d5..852adaf 100644 --- a/test/spilt_string_test.go +++ b/test/spilt_string_test.go @@ -22,3 +22,19 @@ func TestSplit2(t *testing.T) { t.Errorf("want:%v, but got:%v\n", want, got) } } + +func TestSplit3(t *testing.T) { + got := Split("abcef", "bc") + want := []string{"a", "ef"} + if !reflect.DeepEqual(want, got) { + t.Fatalf("want:%v, but got:%v\n", want, got) + } +} + +//基准测试 + +func BenchmarkSplit(b *testing.B) { + for i := 0; i < b.N; i++ { + Split("a:b:c:d:e:f", ":") + } +}