From bc4675d9e877b7e4e29d7832afc52829e894e90c Mon Sep 17 00:00:00 2001 From: zhongyuxiang Date: Mon, 18 Jan 2021 15:01:56 +0800 Subject: [PATCH 1/2] by stage --- .idea/code.iml | 8 ++++++++ README.md | 7 +++++++ 2 files changed, 15 insertions(+) create mode 100644 .idea/code.iml diff --git a/.idea/code.iml b/.idea/code.iml new file mode 100644 index 00000000..c956989b --- /dev/null +++ b/.idea/code.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/README.md b/README.md index d7b02868..bad3f70d 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,10 @@ code ==== Source Code for Go In Action examples + +GO 语言实战 + +GO IN ACTION + +BY + STAGE 9527 \ No newline at end of file From 42713d11929bc5f7125a705c329e1d15038313ed Mon Sep 17 00:00:00 2001 From: zhongyuxiang Date: Tue, 19 Jan 2021 16:48:52 +0800 Subject: [PATCH 2/2] test --- .idea/watcherTasks.xml | 29 +++++++ chapter1/channels/SymLinktest.go | 20 +++++ chapter1/channels/hellochannels.go | 2 +- chapter2/sample/main.go | 6 +- chapter2/sample/matchers/rss.go | 4 +- chapter2/sample/search/feed.go | 6 +- chapter2/sample/search/search.go | 1 + chapter3/wordcount/gowords.txt | 2 +- chapter3/wordcount/wordcount.go | 4 +- test/funvar.go | 28 +++++++ test/gotofun.go | 31 ++++++++ test/nonamefun.go | 67 ++++++++++++++++ test/stringsync.go | 122 +++++++++++++++++++++++++++++ test/test.txt | 3 + test/vartet.go | 38 +++++++++ 15 files changed, 355 insertions(+), 8 deletions(-) create mode 100644 .idea/watcherTasks.xml create mode 100644 chapter1/channels/SymLinktest.go create mode 100644 test/funvar.go create mode 100644 test/gotofun.go create mode 100644 test/nonamefun.go create mode 100644 test/stringsync.go create mode 100755 test/test.txt create mode 100644 test/vartet.go diff --git a/.idea/watcherTasks.xml b/.idea/watcherTasks.xml new file mode 100644 index 00000000..4bb84919 --- /dev/null +++ b/.idea/watcherTasks.xml @@ -0,0 +1,29 @@ + + + + + + + + \ No newline at end of file diff --git a/chapter1/channels/SymLinktest.go b/chapter1/channels/SymLinktest.go new file mode 100644 index 00000000..6e99d688 --- /dev/null +++ b/chapter1/channels/SymLinktest.go @@ -0,0 +1,20 @@ +package main + +import ( + "io/ioutil" + "log" + "os" +) + +// 生成软连接 +func main() { + err := ioutil.WriteFile("original.txt", []byte("hello world"), 0600) + if err != nil { + log.Fatalln(err) + } + + err = os.Symlink("original.txt", "link.txt") + if err != nil { + log.Fatalln(err) + } +} diff --git a/chapter1/channels/hellochannels.go b/chapter1/channels/hellochannels.go index 9282e07d..2345ba44 100644 --- a/chapter1/channels/hellochannels.go +++ b/chapter1/channels/hellochannels.go @@ -9,7 +9,7 @@ var wg sync.WaitGroup func printer(ch chan int) { for i := range ch { - fmt.Printf("Received %d ", i) + fmt.Printf("Received %d \n", i) } wg.Done() } diff --git a/chapter2/sample/main.go b/chapter2/sample/main.go index c1d3b7c4..069256c1 100644 --- a/chapter2/sample/main.go +++ b/chapter2/sample/main.go @@ -4,8 +4,8 @@ import ( "log" "os" - _ "github.com/goinaction/code/chapter2/sample/matchers" - "github.com/goinaction/code/chapter2/sample/search" + _ "./matchers" + "./search" ) // init is called prior to main. @@ -17,5 +17,5 @@ func init() { // main is the entry point for the program. func main() { // Perform the search for the specified term. - search.Run("president") + search.Run("Chinese") } diff --git a/chapter2/sample/matchers/rss.go b/chapter2/sample/matchers/rss.go index 35854548..46c7e210 100644 --- a/chapter2/sample/matchers/rss.go +++ b/chapter2/sample/matchers/rss.go @@ -8,7 +8,7 @@ import ( "net/http" "regexp" - "github.com/goinaction/code/chapter2/sample/search" + "../search" ) type ( @@ -136,5 +136,7 @@ func (m rssMatcher) retrieve(feed *search.Feed) (*rssDocument, error) { // We don't need to check for errors, the caller can do this. var document rssDocument err = xml.NewDecoder(resp.Body).Decode(&document) + //fmt.Println("================") + //fmt.Printf("%#v\n",&document) return &document, err } diff --git a/chapter2/sample/search/feed.go b/chapter2/sample/search/feed.go index ee254ae7..a62559f9 100644 --- a/chapter2/sample/search/feed.go +++ b/chapter2/sample/search/feed.go @@ -3,10 +3,13 @@ package search import ( "encoding/json" "os" + "path/filepath" ) const dataFile = "data/data.json" +const fileDir = "data" + // Feed contains information we need to process a feed. type Feed struct { Name string `json:"site"` @@ -17,7 +20,8 @@ type Feed struct { // RetrieveFeeds reads and unmarshals the feed data file. func RetrieveFeeds() ([]*Feed, error) { // Open the file. - file, err := os.Open(dataFile) + cfgfile := filepath.Join(fileDir, "data.json") + file, err := os.Open(cfgfile) if err != nil { return nil, err } diff --git a/chapter2/sample/search/search.go b/chapter2/sample/search/search.go index df84cf45..68ca6e45 100644 --- a/chapter2/sample/search/search.go +++ b/chapter2/sample/search/search.go @@ -29,6 +29,7 @@ func Run(searchTerm string) { // Launch a goroutine for each feed to find the results. for _, feed := range feeds { // Retrieve a matcher for the search. + //fmt.Println(feeds) matcher, exists := matchers[feed.Type] if !exists { matcher = matchers["default"] diff --git a/chapter3/wordcount/gowords.txt b/chapter3/wordcount/gowords.txt index 91edb5a3..171a52c8 100644 --- a/chapter3/wordcount/gowords.txt +++ b/chapter3/wordcount/gowords.txt @@ -1 +1 @@ -Now is the time for all good gophers to write code. +Now is the time for all good gophers to write code.32131 3 diff --git a/chapter3/wordcount/wordcount.go b/chapter3/wordcount/wordcount.go index 778cf279..a31db64a 100644 --- a/chapter3/wordcount/wordcount.go +++ b/chapter3/wordcount/wordcount.go @@ -6,7 +6,7 @@ import ( "io/ioutil" "os" - "github.com/goinaction/code/chapter3/words" + "../words" ) // main is the entry point for the application. @@ -14,6 +14,8 @@ func main() { filename := os.Args[1] contents, err := ioutil.ReadFile(filename) + //contents, err := os.Open(filename) + if err != nil { fmt.Println("There was an error opening the file:", err) return diff --git a/test/funvar.go b/test/funvar.go new file mode 100644 index 00000000..a1bcd3b7 --- /dev/null +++ b/test/funvar.go @@ -0,0 +1,28 @@ +package main + +import "fmt" + +// opFunc为自定义的类型名字,这里它是一个函数,接收两个值,返回一个值 +type opFunc func(int, int) int + +func add(a, b int) int { + return a + b +} + +// op为变量名字,op_func为自己定义的类型 +func operator(op func(int, int) int, a, b int) int { + return op(a, b) +} + +func operator2(op opFunc, a, b int) int { + return op(a, b) +} + +func main() { + xx := add + result := operator(xx, 10, 10) + fmt.Println(result) // 20 + + result2 := operator2(xx, 10, 10) + fmt.Println(result2) // 20 +} diff --git a/test/gotofun.go b/test/gotofun.go new file mode 100644 index 00000000..d31033fc --- /dev/null +++ b/test/gotofun.go @@ -0,0 +1,31 @@ +package main + +import "fmt" + +func func1() { +LABEL: + for i := 0; i <= 5; i++ { + for j := 0; j <= 5; j++ { + if j == 4 { + continue LABEL // LABEL相当于标志,程序跳到此处执行 + } + fmt.Printf("i is: %d, and j is: %d\n", i, j) + } + } +} + +func func2() { // 相当于for循环 + i := 0 +LABEL: + fmt.Println(i) + i++ + if i == 5 { + return + } + goto LABEL +} + +func main() { + func1() + func2() +} diff --git a/test/nonamefun.go b/test/nonamefun.go new file mode 100644 index 00000000..cb10fe0d --- /dev/null +++ b/test/nonamefun.go @@ -0,0 +1,67 @@ +package main + +import ( + "bufio" + "fmt" + "os" +) + +var ( + //Func就是一个全局匿名函数 + Func = func(a, b int) int { + return a * b + } +) + +func addsum(a, b int) int { + result := func(a1, b1 int) int { + return a1 + b1 + }(a, b) // 定义时就调用 + return result + + //result := func(a1, b1 int) int { + // return a1 + b1 + //} + //return result(a, b) +} + +func testdefer(a, b int) { + defer fmt.Println(a) + defer fmt.Println(b) + a++ + b++ + tmp := a + b + fmt.Println(tmp) + fmt.Println(a) + fmt.Println(b) +} + +func main() { + fmt.Println(addsum(10, 10)) // 20 + res := Func(1, 3) + fmt.Println(res) + + testdefer(1, 5) + + readFileContent("./test.txt") +} + +func readFileContent(filename string) { + if filename == "" { + fmt.Println("请输入文件") + return + } + file, err := os.Open(filename) + if err != nil { + fmt.Println("打开文件失败", err) + return + } + scaner := bufio.NewScanner(file) + for scaner.Scan() { + content := scaner.Text() + if len(content) == 0 { + continue + } + fmt.Println(content) + } +} diff --git a/test/stringsync.go b/test/stringsync.go new file mode 100644 index 00000000..fdb7bd8d --- /dev/null +++ b/test/stringsync.go @@ -0,0 +1,122 @@ +package main + +import ( + "fmt" + "strconv" + "strings" +) + +// 字符串反转 +func ReverseStr(str string) string { + var result string + strLen := len(str) + for i := 0; i < strLen; i++ { + result = result + fmt.Sprintf("%c", str[strLen-i-1]) + } + return result +} + +func main() { + var str1 = "Hello World" + // 测量字符串长度 + result1 := len(str1) + fmt.Println(result1) // 11 + + // 字符串反转 + fmt.Println(ReverseStr(str1)) // dlroW olleH + + // 判断字符串s是否以prefix开头 + // func HasPrefix(s, prefix string) bool + fmt.Println(strings.HasPrefix(str1, "H")) // true + + // 判断字符串s是否以suffix结尾 + // func HasSuffix(s, suffix string) bool + fmt.Println(strings.HasSuffix(str1, "ld")) // true + + // 返回字符串在s中第一个substr实例的索引,如果s中不存在substr,则返回-1 + // func Index(s, substr string) int + fmt.Println(strings.Index("chicken", "en")) // 5 + fmt.Println(strings.Index("chicken", "gg")) // -1 + + // 返回字符串在s中最后一个substr实例的索引,如果s中不存在substr,则返回-1 + //func LastIndex(s, substr string) int + fmt.Println(strings.Index("go gopher", "go")) // 0 + fmt.Println(strings.LastIndex("go gopher", "go")) // 3 + fmt.Println(strings.LastIndex("go gopher", "rodent")) // -1 + + // 字符串替换 + // func Replace(s, old, new string, n int) string + /* + 返回字符串s的副本,其中前n个非重叠的old实例替换为new。 + 如果old为空,则它在字符串的开头和每个UTF-8序列之后匹配,对k-rune字符串产生最多k + 1个替换。 + 如果n <0,则替换次数没有限制。 + */ + fmt.Println(strings.Replace("oink oink oink", "k", "pd", 2)) // oinpd oinpd oink + fmt.Println(strings.Replace("oink oink oink", "oink", "pd", -1)) // pd pd pd + + // 计算字符串中某个字符出现次数 + // func Count(s, substr string) int + /* + 计算字符串s中非重叠substr实例的数量。 + 如果substr是空字符串,则Count返回1 + s中的Unicode代码点数。 + */ + fmt.Println(strings.Count("cheese", "e")) // 3 + fmt.Println(strings.Count("fffwdddd", "")) // 9 + + // 拼接字符串本身 + // func Repeat(s string, count int) string + fmt.Println(strings.Repeat("pd", 2)) // pdpd + + // 字符串全小写 + // func ToLower(s string) string + fmt.Println(strings.ToLower("Gopher")) // gopher + + // 字符串全小大写 + // func ToUpper(s string) string + fmt.Println(strings.ToUpper("Gopher")) // GOPHER + + // 去掉字符串首尾空白字符 + //func TrimSpace(s string) string + fmt.Println(strings.TrimSpace(" \t\n Hello, World \n\t\r\n")) // Hello, World + + //去掉字符串首尾cutset字符 + // func Trim(s string, cutset string) string + fmt.Println(strings.Trim("¡¡¡Hello!¡World!!!", "!¡")) // Hello!¡World + + // 去掉字符串首部cutset字符 + //func TrimLeft(s string, cutset string) string + fmt.Println(strings.TrimLeft("¡¡¡Hello!¡World!!!", "!¡")) // Hello!¡World!!! + + // 去掉字符串尾部cutset字符 + // func TrimRight(s string, cutset string) string + fmt.Println(strings.TrimRight("¡¡¡Hello!¡World!!!", "!¡")) // ¡¡¡Hello!¡World + + // 返回字符串空格分隔的所有子字符串片段 + // func Fields(s string) []string + fmt.Println(strings.Fields(" foo bar baz ")) // [foo bar baz] + + // 返回字符串split分隔的所有子串的slice + // func Split(s, sep string) []string + fmt.Printf("%q\n", strings.Split("foo,bar,baz", ",")) // ["foo" "bar" "baz"] + fmt.Printf("%q\n", strings.Split("a foo a bar a baz", "a ")) // ["" "foo " "bar " "baz"] + fmt.Printf("%q\n", strings.Split(" xyz ", "")) // [" " "x" "y" "z" " "] + fmt.Printf("%q\n", strings.Split("", "pd")) // [""] + + // 用sep把s中的所有元素连接起来,以创建单个字符串 + // func Join(a []string, sep string) string + s := []string{"foo", "bar", "baz"} + fmt.Printf("%q\n", strings.Join(s, ", ")) // "foo, bar, baz" + + // 把一个整数i转成字符串 + //func Itoa(i int)string + i := 10 + str2 := strconv.Itoa(i) + fmt.Printf("%T %q\n", str2, str2) // string "10" + + // 把一个字符串转成整数 + //func Atoi(s string)(int,error) + str3 := "10" + if s, err := strconv.Atoi(str3); err == nil { + fmt.Printf("%T %d\n", s, s) // int 10 + } +} diff --git a/test/test.txt b/test/test.txt new file mode 100755 index 00000000..518cccef --- /dev/null +++ b/test/test.txt @@ -0,0 +1,3 @@ +万水千山只等闲 + +stage \ No newline at end of file diff --git a/test/vartet.go b/test/vartet.go new file mode 100644 index 00000000..f2d57f9d --- /dev/null +++ b/test/vartet.go @@ -0,0 +1,38 @@ +package main + +import "fmt" + +// 方式1:交换两个变量的值 +func swap1(a, b *int) { + *a, *b = *b, *a +} + +// 方式2:交换两个变量的值 +func swap2(a, b int) (int, int) { + return b, a +} + +func main() { + var i int = 10 + var p *int = &i // *int代表指针,&a代表获取a的地址 + *p = 20 // 给指针所指向的值赋值 + fmt.Println(i) // 20 + + a, b := 11, 22 + swap1(&a, &b) + fmt.Println(a, b) // 22 11 + + c, d := 33, 44 + c, d = swap2(c, d) + fmt.Println(c, d) // 44 33 + + var ii int + ii = 10 + fmt.Printf("i的地址:%p\n", &ii) + + var pointer *int + pointer = &ii + fmt.Printf("pointer是一个指针变量:%v\n", pointer) + fmt.Printf("pointer的地址:%p\n", &pointer) + fmt.Printf("pointer指向的值:%v\n", *pointer) +}