Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go,doc: module and documentation on "/doc/code.html" #31840

Closed
shuLhan opened this issue May 5, 2019 · 5 comments
Closed

cmd/go,doc: module and documentation on "/doc/code.html" #31840

shuLhan opened this issue May 5, 2019 · 5 comments

Comments

@shuLhan
Copy link
Contributor

shuLhan commented May 5, 2019

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

$ go version
go version devel +c85b81238f Sat May 4 13:13:42 2019 +0700 linux/amd64

Does this issue reproduce with the latest release?

Yes.

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

go env Output
$ go env
GOARCH="amd64"
GOBIN="/home/ms/bin"
GOCACHE="/home/ms/.cache/go-build"
GOENV="/home/ms/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ms/go"
GOPROXY="https://proxy.golang.org"
GOROOT="/home/ms/share/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/ms/share/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
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-build900266218=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Following the instruction on https://golang.org/doc/code.html from the beginning until "Your first program", the go install command on both examples will fail. Here is the log from beginning to the end,

11:19 ~/go/src/github.com/shuLhan/sandbox/hello
master ms 0 % vim hello.go 

11:29 ~/go/src/github.com/shuLhan/sandbox/hello
master ms 0 % rm -rf .git

11:29 ~/go/src/github.com/shuLhan/sandbox/hello
master ms 0 % git init .
Initialized empty Git repository in /home/ms/go/src/github.com/shuLhan/sandbox/hello/.git/

11:29 ~/go/src/github.com/shuLhan/sandbox/hello
master ms 0 % git add hello.go 

11:29 ~/go/src/github.com/shuLhan/sandbox/hello
master ms 0 % git commit -m "initial commit"
[master (root-commit) eb2b97d] initial commit
 1 file changed, 7 insertions(+)
 create mode 100644 hello.go

11:29 ~/go/src/github.com/shuLhan/sandbox/hello
master ms 0 % go install
go: cannot find main module, but found .git/config in /home/ms/go/src/github.com/shuLhan/sandbox/hello
        to create a module there, run:
        go mod init

11:32 ~/go/src/github.com/shuLhan/sandbox/hello
master ms 1 % ls        
hello.go

11:32 ~/go/src/github.com/shuLhan/sandbox/hello
master ms 0 % export GOPATH=$(go env GOPATH)

11:36 ~/go/src/github.com/shuLhan/sandbox/hello
master ms 0 % go install                    
go: cannot find main module, but found .git/config in /home/ms/go/src/github.com/shuLhan/sandbox/hello
        to create a module there, run:
        go mod init

11:36 ~/go/src/github.com/shuLhan/sandbox/hello
master ms 1 % echo $GOPATH
/home/ms/go

11:36 ~/go/src/github.com/shuLhan/sandbox/hello
master ms 0 % go run hello.go 
Hello, world.

11:39 ~/go/src/github.com/shuLhan/sandbox/hello
master ms 0 % go install github.com/shuLhan/sandbox/hello
can't load package: package github.com/shuLhan/sandbox/hello: unknown import path "github.com/shuLhan/sandbox/hello": cannot find module providing package github.com/shuLhan/sandbox/hello

11:43 ~/go/src/github.com/shuLhan/sandbox/hello
master ms 1 % echo $GOPATH                               
/home/ms/go

What did you expect to see?

Both go install commands, using import path and when running inside package directory, fail to run.

What did you see instead?

Both go install commands should successfully running without error.

Notes

I am not sure if this documentation or module issue, but I am prefer, if its possible, that the Go module is not aggressive as it is now. I have try to discuss this on golang-nuts [1] with one suggestion to raise this as an issue.

[1] https://groups.google.com/forum/#!topic/golang-nuts/48ASOqXkwa8

@shuLhan shuLhan changed the title doc: module and documentation on "/doc/code.html" cmd/go,doc: module and documentation on "/doc/code.html" May 5, 2019
@thepudds
Copy link
Contributor

thepudds commented May 5, 2019

https://golang.org/doc/code.html has not yet been updated for modules. (See #28215; if you are looking for an introduction to modules, you can see https://blog.golang.org/using-go-modules for example).

Is the suggestion here that you would like go mod init to be optional before doing something like go install?

(Note that I think go run is already more permissive, but I am not 100% sure).

@shuLhan
Copy link
Contributor Author

shuLhan commented May 6, 2019 via email

@bcmills
Copy link
Contributor

bcmills commented May 6, 2019

#31857 proposes to move the default back to GO111MODULE=auto, but make that mode somewhat more aggressive in terms of when module mode triggers.

That would get you the desired behavior when GOPATH is set but there is no go.mod file in the working directory within GOPATH/src.

@bcmills
Copy link
Contributor

bcmills commented May 6, 2019

A heuristic based on whether the current package name is main does not seem like a good idea, for two reasons:

  1. Package main may well import other packages from outside the standard library, and if it does we need to know how to resolve them. (A “cliff” as soon as you add a non-standard-library import does not seem like a great user experience.)
  2. Certain operations (such as those in the reflect and runtime packages) are sensitive to package path, so we generally need to know the actual package path of main regardless of where the source code was found. Outside of GOPATH, the only way we know the package path is by looking at the module directive in the go.mod file.

@bcmills
Copy link
Contributor

bcmills commented May 6, 2019

Duplicate of #31857

@bcmills bcmills closed this as completed May 6, 2019
@bcmills bcmills marked this as a duplicate of #31857 May 6, 2019
@golang golang locked and limited conversation to collaborators May 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants