Go Generate Vs Go Build!

231 views
Skip to first unread message

Compiler

unread,
Dec 17, 2017, 4:33:55 PM12/17/17
to golang-nuts
Hello,

Go Generate Vs Go Build!?!

guest@Base:~/Gits/go-hello$ ls
hello  hello.go
guest@Base:~/Gits/go-hello$ cat hello.go
package main

import "fmt"

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

guest@Base:~/Gits/go-hello$ go build hello.go
guest@Base:~/Gits/go-hello$ go generate hello.go
guest@Base:~/Gits/go-hello$ ./hello
Hello, World

Compiler

unread,
Dec 17, 2017, 4:45:12 PM12/17/17
to golang-nuts
  • https://en.wikipedia.org/wiki/Go_(programming_language)
  • go build, which builds Go binaries using only information in the source files themselves, no separate makefiles
  • go test, for unit testing and microbenchmarks
  • go fmt, for formatting code
  • go get, for retrieving and installing remote packages
  • go vet, a static analyzer looking for potential errors in code
  • go run, a shortcut for building and executing code
  • godoc, for displaying documentation or serving it via HTTP
  • gorename, for renaming variables, functions, and so on in a type-safe way
  • go generate, a standard way to invoke code generators

Please explain me.

Ain

unread,
Dec 17, 2017, 5:11:03 PM12/17/17
to golang-nuts


On Sunday, December 17, 2017 at 11:45:12 PM UTC+2, Compiler wrote:
  • https://en.wikipedia.org/wiki/Go_(programming_language)
  • go build, which builds Go binaries using only information in the source files themselves, no separate makefiles
  • go test, for unit testing and microbenchmarks
  • go fmt, for formatting code
  • go get, for retrieving and installing remote packages
  • go vet, a static analyzer looking for potential errors in code
  • go run, a shortcut for building and executing code
  • godoc, for displaying documentation or serving it via HTTP
  • gorename, for renaming variables, functions, and so on in a type-safe way
  • go generate, a standard way to invoke code generators

Please explain me.

Have you seen https://golang.org/cmd/go/


HTH
ain

Dave Cheney

unread,
Dec 17, 2017, 7:15:33 PM12/17/17
to golang-nuts

howar...@gmail.com

unread,
Dec 20, 2017, 9:55:41 AM12/20/17
to golang-nuts
Go generate is not needed in your example. Go build is what is producing the hello executable.

Go generate is used to run special commands specified by comments in the .go files - these commands could produce new .go files, package binaries into binhex or base64 encoded strings in .go files for inclusion in the executable, produce inter-process communication interface descriptions from go structs, embedding version numbers without having to remember to set -ldflags, automating the collection of API information from the internet, etc.

When it is run and there are no .go files containing generate commands, it is doing nothing but scanning the files and exiting. In general, pretty much anything you 'go get' should have included any generated files in the downloaded data, so running it again is usually not needed, unless the README indicates that it is.

Howard
Reply all
Reply to author
Forward
0 new messages