Skip to content

proposal: go.mod's replace in .go import section, not in go.mod #37743

@VertexToEdge

Description

@VertexToEdge

What version of Go are you using (go version)?

$ go version
go version go1.13.6 linux/amd64

Does this issue reproduce with the latest release?

FeatureRequest

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/vertex/.cache/go-build"
GOENV="/home/vertex/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/vertex/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build816677868=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Go module is a very simple and explicit way to manage dependency and package.
but I want to FeatureRequest about "replace in import section" because of enhancing source readability and convenience of coding.

I usually use replace feature when import local modules.

let me give you an example about importing local module and read the code.

there is example source tree

/example
    ㄴ/foo
        ㄴfoo.go
        ㄴgo.mod
        ㄴgo.sum
    ㄴmain.go
    ㄴgo.mod
    ㄴgo.sum


case 1: Importing local module

When I want to import local module foo
i will do edit two file. /example/main.go and /example/go.mod

/example/main.go:

package main

import (
    "fmt"
    "github.com/VertexToEdge/example/foo"  // <----- 1.import foo
)

func main() {
    fmt.Println("Hello World")
    foo.Foo()                              //  <----- 2. use foo
}

and also edit /example/go.mod for replace github.com/VertexToEdge/example/foo
to import module in local path.

/example/go.mod

module github.com/VertexToEdge/example

go 1.13

 // 3. replace to import local module
replace github.com/VertexToEdge/example/foo => ./foo 


case 2: Read the code

a third party got this code.

/example/main.go:

package main

import (
    "fmt"
    "github.com/VertexToEdge/example/foo"  // <----- 2. what is foo?
)

func main() {
    fmt.Println("Hello World")
    foo.Foo()                              //  <----- 1. what is this? foo?
}

In order to assure what exactly github.com/VertexToEdge/example/foo do,
he will have to open /example/go.mod.

/example/go.mod

module github.com/VertexToEdge/example

go 1.13

 // 3. ah ha! this is local module!
replace github.com/VertexToEdge/example/foo => ./foo  

What did you expect to see?

when replace module.

just replace in import section, not in go.mod
/example/main.go:

package main

import (
    "fmt"
    "github.com/VertexToEdge/example/foo" => ./foo
)

func main() {
    fmt.Println("Hello World")
    foo.Foo()
}

them, code writer and reader need not to open to read other file(go.mod)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions