美文网首页
golang官方练习:单词统计

golang官方练习:单词统计

作者: jiaxiaolei | 来源:发表于2022-11-20 16:26 被阅读0次
    
    
    
    [root@sltvb7v2wy3 jia_maps]# cat exercise_maps.go
    package main
    
    import (
            "golang.org/x/tour/wc"
    )
    
    func WordCount(s string) map[string]int {
            return map[string]int{"x": 1}
    }
    
    func main() {
            wc.Test(WordCount)
    }
    
    
    
    [root@sltvb7v2wy3 jia_maps]# gor exercise_maps.go
    exercise_maps.go:4:2: no required module provides package golang.org/x/tour/wc: go.mod file not found in current directory or any parent directory; see 'go help modules'[root@sltvb7v2wy3 jia_maps]# go install golang.org/x/tour/wc
    go: 'go install' requires a version when current directory is not in a module
            Try 'go install golang.org/x/tour/wc@latest' to install the latest version
    
    [root@sltvb7v2wy3 jia_maps]# go install golang.org/x/tour/wc@latest
    package golang.org/x/tour/wc is not a main package
    
    
    
    [root@sltvb7v2wy3 jia_maps]# go get  golang.org/x/tour/wc@latest
    go: go.mod file not found in current directory or any parent directory.
            'go get' is no longer supported outside a module.
            To build and install a command, use 'go install' with a version,
            like 'go install example.com/cmd@latest'
            For more information, see https://golang.org/doc/go-get-install-deprecation
            or run 'go help get' or 'go help install'.
    
    
    
    [root@sltvb7v2wy3 jia_maps]# go mod  init example.com/exercise_maps.go
    go: creating new go.mod: module example.com/exercise_maps.go
    go: to add module requirements and sums:
            go mod tidy
    [root@sltvb7v2wy3 jia_maps]#  go mod tidy
    go: finding module for package golang.org/x/tour/wc
    go: found golang.org/x/tour/wc in golang.org/x/tour v0.1.0
    [root@sltvb7v2wy3 jia_maps]# gor exercise_maps.go
    FAIL
     f("I am learning Go!") =
      map[string]int{"x":1}
     want:
      map[string]int{"Go!":1, "I":1, "am":1, "learning":1}
    
    [root@sltvb7v2wy3 jia_maps]# 
    
    
    
    
    
    [root@sltvb7v2wy3 jia_maps]# cat  exercise_maps.go
    package main
    
    import (
            "strings"
            "golang.org/x/tour/wc"
    )
    
    func WordCount(s string) map[string]int {
            m := make(map[string]int)
            for _, word := range strings.Fields(s) {
                    m[word] += 1
            }
            return m
    }
    
    func main() {
            wc.Test(WordCount)
    }
    
    
    
    
    [root@sltvb7v2wy3 jia_maps]# gor  exercise_maps.go
    FAIL
     f("I am learning Go!") =
      map[string]int{"x":1}
     want:
      map[string]int{"Go!":1, "I":1, "am":1, "learning":1}[root@sltvb7v2wy3 jia_maps]# vim exercise_maps.go
    [root@sltvb7v2wy3 jia_maps]# gor  exercise_maps.go
    PASS
     f("I am learning Go!") = 
      map[string]int{"Go!":1, "I":1, "am":1, "learning":1}
    PASS
     f("The quick brown fox jumped over the lazy dog.") = 
      map[string]int{"The":1, "brown":1, "dog.":1, "fox":1, "jumped":1, "lazy":1, "over":1, "quick":1, "the":1}
    PASS
     f("I ate a donut. Then I ate another donut.") = 
      map[string]int{"I":2, "Then":1, "a":1, "another":1, "ate":2, "donut.":2}
    PASS
     f("A man a plan a canal panama.") = 
      map[string]int{"A":1, "a":2, "canal":1, "man":1, "panama.":1, "plan":1}
    
    
    
    
    
    
    
    
    
    
    
    
    

    相关文章

      网友评论

          本文标题:golang官方练习:单词统计

          本文链接:https://www.haomeiwen.com/subject/ombbxdtx.html