Go Tutorial Part 1 - Introduction
Contents
1 Why Go?
- Golang is an open source statics typed, compiled language.
- It finds a sweet-spot between easy to use and stay powerful
- This will be a series of posts that will help understand the language and it’s powers
2 Setting Up
- Download and Install Go from the official website
- Type GO in terminal to make sure go was installed correctly
- Install a code editor
- If VSCODE then install the go extension and make sure the addons are installed with it.
3 A simple Go program
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
Now save the program as hello.go
4 Running the program
- Open a Terminal and navigate to the directory in which you saved the program
- Type
go run hello.go
- You should see
Hello World!
printed
5 Diving Deep
5.1 package main
- package is like a workspace or project
- package is a collection of source code files.
- package per application
- Each file in the package should declare the package on the top like
package main
- Two types of packages - executable and reuseable
- Executable package:
- Generates a file that we can run
- Has package name as
main
- Must have a function named
main
- Reuseable package:
- To be imported and used in other projects
- Does not create an executable when build
- Does not use the package name as main
5.2 import "fmt"
- Giving our package access to code written in other package
- fmt is a package that helps format the code better
- Check official docs for more details.
5.3 func main
func
is short for function- Similar in functionality to function in any other programming language