Table of contents
Let’s write a hello world programme in go
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
In Go, you don’t manually need to write or remove import statements yourself. As soon as we save the code, Go automatically manages the package imports required in the code.
Command to Run the Go File
go run <file_name>
This command will only execute the code. It will not generate any executable (.exec) files that can be executed separately.
Go Docs and GOPATH
go help
Lists all the commands available for Go.
go help <command_name>
Provides details about a specific command.
GOPATH
The Go path is used to resolve import statements and is implemented by and documented in the
go/build
package.If the environment variable is unset,
GOPATH
defaults to a subdirectory named "go" in the user's home directory ($HOME/go
on Unix,%USERPROFILE%\\\\go
on Windows), unless that directory holds a Go distribution. Rungo env GOPATH
(case-sensitive) to see the currentGOPATH
.You can see various packages inside the
/pkg/mod
directory ofGOPATH
.
The Semicolon Rule
In Go, like C, the formal grammar uses semicolons to terminate statements, but unlike in C, those semicolons do not appear in the source code. Instead, the lexer uses a simple rule to insert semicolons automatically as it scans, so the input text is mostly free of them. The lexer always inserts a semicolon after the token.
- What is a Lexer?
A lexer, also known as a scanner or tokenizer, breaks up the source code into meaningful tokens. It is the first phase of a compiler or interpreter.