Create from command line

Overview

We can enter golang officially after completing Golang installation, and the two current mainstream editors are Goland VSCode; in addition to this, we can create the Golang project from the terminal. This paper will describe how to create a golang project using the command line.

Create project

  1. # Create a golang project
  2. ~ mkdir -p ~/workspace/helloworld/ && cd ~/workspace/helloworld
  3. helloworld # crete go module
  4. helloworld go mod init helloworld
  5. go: creating new go.mod: module helloworld
  6. helloworld # create main.go
  7. helloworld touch main.go
  8. helloworld # add code
  9. helloworld echo 'package main
  10. >
  11. > import "fmt"
  12. >
  13. > func main() {
  14. > fmt.Println("Hello World!")
  15. > }
  16. > ' > main.go

Run main.go

  1. go run main.go
  2. Hello World!