Go syntax is clean, minimal, and easy to understand. It drops many complex features of other languages, making code more readable and maintainable. Letโs explore the basic structure!
๐ Tip: Unlike Python, Go does not use indentation for blocks. Instead, it uses curly braces { }.
package main
import "fmt"
func main() {
fmt.Println("Welcome to Go Syntax!")
}
Explanation:
package main: Required to build an executable programimport "fmt": Imports the formatting packagefunc main(): The main function where execution startsfmt.Println(): Prints text with a new line
var name string = "Go"
age := 10
fmt.Println("Name:", name)
fmt.Println("Age:", age)
Explanation:
var name string = "Go": Explicit variable declarationage := 10: Short variable declaration (type inferred)// This is a single-line comment /* This is a multi-line comment */
Use comments to explain your code. Go supports both single-line and multi-line comments.
Go has several basic types:
string โ sequence of charactersint, float64 โ numbersbool โ true/falsearray, slice, map, struct โ collections
if age > 18 {
fmt.Println("Adult")
} else {
fmt.Println("Minor")
}
No parentheses needed in if conditions! Just curly braces.
for
for i := 1; i <= 5; i++ {
fmt.Println("Count:", i)
}
Note: Go has only one loop keyword: for. It replaces while too.
The main() function ends when all its code is executed. Thereโs no return 0; like in C.
๐ Summary: Go syntax is clean and efficient. You use packages, functions, and clear blocks. Thereโs less boilerplate and more readable code!
Head to Go Playground and try writing a program that:
ifHelp others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!