As a first step of learning, lets try to run a "Hello World!" in Go by simply following the below steps
- Create a folder to save the program and organize your future Go projects
- Launch any code editor ie VS code and navigate to the folder you had created
- Create "hello_world.go" and copy paste the below code and save it
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Below is the short explanation of the above code
- package main - Every Go program starts with a package declaration; main creates an executable
- import "fmt" - Imports the formatting package for input/output operations
- func main() - The program's entry point where execution begins
- fmt.Println() - Prints text to console with automatic newline
Now go to terminal and run the program as "go run hello-world.go"
The code will be first complied and executed. you will see "Hello, World!" printed out.
Now you have successfully created and executed a program in Go successfully
Follow the below basic guidelines of Go Programming while writing the code
Code Organization
- One package per directory - Keep related code in same folder
- File naming - Use lowercase with underscores: hello_world.go
- main package - Only in executable programs, never in libraries
Formatting & Style
- Use go fmt - Automatic formatting (no debates on brace positions)
- Indentation - Tabs, not spaces (Go's default)
- Line length - No hard limit, but keep readable
- Imports - Grouped: standard library first, then third-party
Key Syntax Rules
- Semicolons - Not needed (compiler inserts them)
- Braces - Opening brace must be on same line
- Types - Come after variable name: var x int
- Public/Private - Capitalized = exported, lowercase = private
- No unused variables - Compiler error
- No unused imports - Compiler error