Properties of GO
- Statically typed language - declare types explicitly or must be inferred, and types cannot change without type conversion
- Strongly typed language - operation allowed to be performed on a variable depends on the types
- Compiled language - converts into machine code instead of interpreted as it is running
- Fast compile time
- Built in concurrency - GoRoutines
- Simplicity - concise syntax and garbage collection
Structure of Go code
- Package: folder that contains a collection of Go files
- Module: collection of packages
- It is what is created when we initialise a module
Initialising a Go Project
go mod init [project name]
- usually instead of project name, it will be the location of the GitHub repository
Syntax
- Specify the package the Go file it belongs to with
package [name]- Name used must be the same for all files within the folder
- All variables and packages declared must be used, else the compiler throws an error
package mainis a special package name that tells the compiler to find the entry point function (main) here
package main
import "fmt"
func main() {
fmt.Println("Hello World")
}Compiling and running a program
go build main.go
./mainwhich is equivalent to go run main.go