编写go代码初识

195 阅读1分钟

go mod

选择mod路径和创建mod文件

$ mkdir hello # Alternatively, clone it if it already exists in version control.
$ cd hello
$ go mod init example.com/user/hello
    go: creating new go.mod: module example.com/user/hello
$ cat go.mod
    module example.com/user/hello

    go 1.16
$

创建代码文件hello.go

package main

import "fmt"

func main() {
        fmt.Println("Hello, world.")
}

创建和安装项目

$ go install example.com/user/hello
副作用:
    mac,linux:$HOME/go/bin/hello
    Windows: %USERPROFILE%\go\bin\hello.exe

设置和删除默认go环境

$ go env -w GOBIN=/somewhere/else/bin
$ go env -u GOBIN

工作目录支持的命令形式

$ go install example.com/user/hello
$ go install .
$ go install

设置path

# Windows users should consult https://github.com/golang/go/wiki/SettingGOPATH
# for setting %PATH%.
$ export PATH=$PATH:$(dirname $(go list -f '{{.Target}}' .))
$ hello
    Hello, world.
$

git 代码版本控制

$ git init
    Initialized empty Git repository in /home/user/hello/.git/
$ git add go.mod hello.go
$ git commit -m "initial commit"
    [master (root-commit) 0b4507d] initial commit
     1 file changed, 7 insertion(+)
     create mode 100644 go.mod hello.go
$

创建本地模块

创建目录与模块文件

    mkdir  $HOME/hello/morestrings
    cd   $HOME/hello/morestrings
    touch reverse.go
   
   reverse.go
     
   package morestrings
   
   func ReverseRunes(s string) string {
        r := []rune(s)
        for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
                r[i], r[j] = r[j], r[i]
        }
        return string(r)
    }

build模块

     $ cd $HOME/hello/morestrings
     $ go build
     

修改$HOME/hello/hello.go

    package main

    import (
        "fmt"

        "example.com/user/hello/morestrings"
    )

    func main() {
        fmt.Println(morestrings.ReverseRunes("!oG ,olleH"))
    }

安装项目

   go install example.com/user/hello
   $ hello

引用远程模块

修改main代码

package main

import (
"fmt"

"example.com/user/hello/morestrings"
"github.com/google/go-cmp/cmp"
)

func main() {
fmt.Println(morestrings.ReverseRunes("!oG ,olleH"))
fmt.Println(cmp.Diff("Hello World", "Hello Go"))
}

更新mod依赖

   $ go mod tidy
        go: finding module for package github.com/google/go-cmp/cmp
        go: found github.com/google/go-cmp/cmp in github.com/google/go-cmp v0.5.4
   $ go install example.com/user/hello
   $ hello
    Hello, Go!
          string(
        - 	"Hello World",
        + 	"Hello Go",
          )
    $ cat go.mod
        module example.com/user/hello

        go 1.16

        require github.com/google/go-cmp v0.5.4
    $

清除mod依赖下载到本地的模块

   go clean -modcache

测试

要求

     文件名:xxx_test.go
     函数名:TestXXX
            func (t *testing.T) 

创建目录和文件

    $HOME/hello/morestrings/reverse_test.go
    package morestrings

    import "testing"

    func TestReverseRunes(t *testing.T) {
            cases := []struct {
                    in, want string
            }{
                    {"Hello, world", "dlrow ,olleH"},
                    {"Hello, 世界", "界世 ,olleH"},
                    {"", ""},
            }
            for _, c := range cases {
                    got := ReverseRunes(c.in)
                    if got != c.want {
                            t.Errorf("ReverseRunes(%q) == %q, want %q", c.in, got, c.want)
                    }
            }
    }

运行测试

     $ go test
        PASS
        ok  	example.com/user/morestrings 0.165s
      $