Home > Software engineering >  SIMPLE godoc Hello world
SIMPLE godoc Hello world

Time:12-30

Trying to serve a godoc on a simple, flat code folder. The online docs do not explain how to achieve this SIMPLE task.

So, creating this simple structure,

/tmp/testgodoc$ tree
.
└── src
    ├── main  (just the binary)
    └── main.go

1 directory, 2 files

where main.go is simply

/tmp/testgodoc$ cat src/main.go
// Hello godoc
package main

import "fmt"

// Say Hello
func main() {
    fmt.Println("Hello")
}

When running either in GOPATH or module modes, opening localhost:6060 in a browser does not give the expected result of documenting current folder.

Running in module mode give this output and result:

/tmp/testgodoc$ ~/go/bin/godoc  -goroot=. -http=:6060
using module mode; GOMOD=/dev/null
(when Ctrl-C:) cannot find package "." in:
        /src/main
^C

module mode result

And running in GOPATH mode seems to point to the local standard library:

/tmp/testgodoc$ GO111MODULE=off ~/go/bin/godoc  -goroot=. -http=:6060
using GOPATH mode
^C

GOPATH mode result

CodePudding user response:

You should put your main package into a subdirectory, maybe like this:

~/go/src/testGoDoc$ tree
├── cmd
│   └── main.go
├── go.mod
└── pkg
    └── test1
        └── test_package.go

and by this you can run both commands:

godoc -http=:6060 #http://localhost:6060/pkg/<module name inside go.mod>/

and

GO111MODULE=off godoc -http=:6060 #http://localhost:6060/pkg/testGoDoc/
  • Related