go-mod

🏄‍♂️ 用来学习Go语言的mod包管理工具,包括go1.18新特性workspaces使用。 并且提供包和笔记分享学习

Stars
3

Go-mod & Workspaces

[toc]

go mod

  1. GOPATH

  • .

    Println("hello word")
    

Go-mod

  • GOPATH



  • replacegoproxy

~


go-mod

mkdir go-mod
cd go-mod
mkdir hello


PS C:\Users\smile\Desktop\\code\go-mod> go mod init go-mod
go: creating new go.mod: module go-mod
go: to add module requirements and sums:
        go mod tidy

module go-mod

go 1.18

hello.go

package main

import "fmt"

func main() {
	fmt.Println("hello word")
}

PS C:\Users\smile\Desktop\\code\go-mod\hello> go run . 
hello word

PS C:\Users\smile\Desktop\\code\go-mod> tree
   PATH 
 DE95-1D97
C:.
hello
main

PS C:\Users\smile\Desktop\\code\go-mod> cd .\main\
PS C:\Users\smile\Desktop\\code\go-mod\main> New-Item main.go            

    : C:\Users\smile\Desktop\\code\go-mod\main

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         2022/5/25     21:32              0 main.go

,task.go

PS C:\Users\smile\Desktop\\code\go-mod> mkdir models


    : C:\Users\smile\Desktop\\code\go-mod


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----         2022/5/25     22:00                models

PS C:\Users\smile\Desktop\\code\go-mod> New-Item task.go

    : C:\Users\smile\Desktop\\code\go-mod

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----         2022/5/25     22:02              0 task.go

package main

import (
	. "fmt"
	"go-mod/hello"
	"go-mod/models"
)

func main() {
	Println("main")
	hello.Hello()
	Println(models.Name)
	//hello.Hello()
}

PS C:\Users\smile\Desktop\\code\go-mod> go run .\main.go
main
hello word
test

modmod~

github

package models

import "fmt"
import "github.com/astaxie/beego"

var Name = "test"

func init() {
	fmt.Println("")
}

func main() {
	beego.Run()
}

PS C:\Users\smile\Desktop\\code\go-mod>  go get github.com/astaxie/beego

go-mod

module go-mod

go 1.18

require (
	github.com/astaxie/beego v1.12.3 // indirect
	github.com/beorn7/perks v1.0.1 // indirect
	github.com/cespare/xxhash/v2 v2.1.1 // indirect
	github.com/golang/protobuf v1.4.2 // indirect
	github.com/hashicorp/golang-lru v0.5.4 // indirect
	github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
	github.com/prometheus/client_golang v1.7.0 // indirect
	github.com/prometheus/client_model v0.2.0 // indirect
	github.com/prometheus/common v0.10.0 // indirect
	github.com/prometheus/procfs v0.1.3 // indirect
	github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 // indirect
	golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550 // indirect
	golang.org/x/net v0.0.0-20190620200207-3b0461eec859 // indirect
	golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 // indirect
	golang.org/x/text v0.3.0 // indirect
	google.golang.org/protobuf v1.23.0 // indirect
	gopkg.in/yaml.v2 v2.2.8 // indirect
)

go sum

github

  1. githubGo-mod
  2. go mod init github.com/3293172751/go-mod
  3. readme.m

go workspace

Why you need it

::: tip sealosGoWorkspaces

sealosgo1.18workspacego work usr -r .rootworkspace

:::

Go :

() bug /

mod

module nsddd.top/mszlu-common

go 1.19

-----------

module nsddd.top/fly-fish

go 1.19

mszlu-common/utils.go

package utils

import (
	"fmt"
)

func Print() {
	fmt.Println("utils.go")

	//fly-fishmain.go
 
}

fly-fish/main.go

package main

import "fmt"

func main() {
	fmt.Println("fly-fishmain.go")
}

mod work

go work init ./fly-fish

fly-fish

go work use ./mszlu-common

go.work

go 1.19

use (
	./fly-fish
	./mszlu-common
)

utils

fly-fish/main.go

package main

import (
	"fmt"

	utils "nsddd.top/mszlu-common"
)

func main() {
	fmt.Println("fly-fishmain.go")
	utils.Print()
}

[Running] go run "c:\Users\smile\Desktop\test\fly-fish\main.go"
fly-fishmain.go
utils.go

go work

  • go.work git
  • : $GOPATH go.work
  • go work init go.work

go.work : go work init ./hello hello hello go mod (./hello/go.mod )

  • go work use

: go work use ./example go work use ./example ./example1 go work use -r ./example ./example go work edit -dropuse=./example

  • go work edit go.work

edit go.work : go work edit -fmt go.work go.work go work edit -replace=github.com/link1st/example=./example go.work go work edit -dropreplace=github.com/link1st/example go work edit -use=./example go.work go work edit -dropuse=./example go.work

  • go work sync
  • go env GOWORK

go.work GOWORK

> go env GOWORK
$GOPATH/src/link1st/link1st/workspaces/go.work

go.work

  • go.mod Go
  • :
go 1.18

use (
    ./hello
    ./example
)

replace (
    github.com/link1st/example => ./example1
)

use

  • go work use hello go.work
  • use replaces
# 
use ./hello

# 
use (
    ./hello
    ./example
)

replaces

  • replaces go.mod
  • replaces use

: go: workspace module github.com/link1st/example is replaced at all versions in the go.work file. To fix, remove the replacement from the go.work file or specify the version at which to replace the module.

use replace

go 1.18

use (
    ./hello
    ./example
)

replace (
    github.com/link1st/example => ./example
)

go.work go.mod

  • go.work go.mod replace go.work go.mod

go.mod example

replace (
    github.com/link1st/example => ./example1
)

go.work example1

replace (
    github.com/link1st/example => ./example1
)
  • go.work example1 go.work

  • Go 1.18 go run go build
  • GOWORK go.work
export GOWORK="~/go/src/test/go.18/workspace/go.work"

  • Go GOWORK off
export GOWORK=off

  • hello github.com/link1st/example ( example)
  • hello
mkdir hello
cd hello
#  go mod  go.mod 
go mod init github.com/link1st/link1st/workspaces/hello
# 
go get github.com/link1st/example
#  main 
vim main.go
  • main.go
// Package main main go 
// 
package main

import (
    "flag"
    "fmt"

    "github.com/link1st/example/stringutil"
)

var (
    str = ""
)

func init() {
    flag.StringVar(&str, "str", str, "")
    flag.Parse()
}

func main() {
    if str == "" {
        fmt.Println(": go run main.go -str hello")
        fmt.Println("str ")
        flag.Usage()
        return
    }

    // 
    str = stringutil.Reversal(str)
    // 
    fmt.Println(str)
    return
}
  • go run main.go -str "hello world" go run github.com/link1st/link1st/workspaces/hello -str "hello world" hello world
> go run main.go -str "hello world"
dlrow olleh
  • example ****
#  common 
#  example 
git clone [email protected]:link1st/example.git
#  example  
  • vim example/stringutil/to_upper.go
// Package stringutil stringutil
package stringutil

import (
    "unicode"
)

// ToUpper 
func ToUpper(s string) string {
    r := []rune(s)
    for i := range r {
        r[i] = unicode.ToUpper(r[i])
    }
    return string(r)
}
  • git Go
#  go.work 
go work init  ./hello ./example
#  go.work 
cat go.work
go 1.18

use (
    ./example
    ./hello
)
  • hello vim main.go
func main() {
    ...

    // 
    str = stringutil.Reversal(str)
    // 
    str = stringutil.ToUpper(str)
    // 
    fmt.Println(str)
    
    ...
}

**** git

go run main.go -str "hello world"
DLROW OLLEH

  • Go

Go 1.18

Go 1.18 is released!

Tutorial: Getting started with multi-module workspaces