Re:Learn Go: Hand-crafted Go exercises and examples
by efiecho on Sunday May 17, 2020 @02:57AM
(#23210163)
I have not yet been able to find a simple "Hello World" example of starting a new project with Go modules instead of $GOPATH.
[ Parent
| on HN ]
Re:Learn Go: Hand-crafted Go exercises and examples
by triztian on Sunday May 17, 2020 @03:06AM
(#23210213)
Hey there I found modules to be confusing coming from GOPATH too, but they're straight forward, super barebones example:
#!/bin/bash
cd $HOME
# Create project directory
mkdir hellogo
cd hellogo
# Init project modfile
go mod init example.com/hellogo
# Create the main source file
# Uses bash's heredoc
cat << EOF > ./main.go
package main
import "fmt"
func main() {
fmt.Print("Hello world")
}
EOF
# Build it
go build
# Run it
./hellogo
Re:Learn Go: Hand-crafted Go exercises and examples
by sk0g on Sunday May 17, 2020 @05:16AM
(#23210752)
You can also skip building, and run directly using `go run`!
Re:Learn Go: Hand-crafted Go exercises and examples
by hans0l074 on Sunday May 17, 2020 @03:11AM
(#23210235)
Actually, the official Go Blog has a decent "Hello World" example[0] for go modules - I've used this before, had to double check.
[0] https://blog.golang.org/using-go-modules
Re:Learn Go: Hand-crafted Go exercises and examples
by JoshuaRLi on Monday May 18, 2020 @02:14PM
(#23226221)
go mod init github.com/your/project
Then, go get -v will download your dependencies to GOPATH, and (re)generate go.mod, go.sum. For info on how to update dependencies, read `go help get`.
When using modules (presence of a valid go.mod file), GOPATH won't be used for resolving imports. So you don't have to actually work in GOPATH anymore, dependencies are just stored there.