GO Tutorial



SYNTAX IN GO


๐Ÿงพ Go Syntax: The Building Blocks

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 { }.

๐Ÿ“Œ 1. Basic Go Program Structure

package main

import "fmt"

func main() {
    fmt.Println("Welcome to Go Syntax!")
}
  

Explanation:

  • package main: Required to build an executable program
  • import "fmt": Imports the formatting package
  • func main(): The main function where execution starts
  • fmt.Println(): Prints text with a new line

๐Ÿงฎ 2. Declaring Variables

var name string = "Go"
age := 10

fmt.Println("Name:", name)
fmt.Println("Age:", age)
  

Explanation:

  • var name string = "Go": Explicit variable declaration
  • age := 10: Short variable declaration (type inferred)

๐Ÿงพ 3. Comments in Go

// 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.

๐Ÿง  4. Data Types in Go

Go has several basic types:

  • string โ€“ sequence of characters
  • int, float64 โ€“ numbers
  • bool โ€“ true/false
  • array, slice, map, struct โ€“ collections

๐ŸŒ€ 5. Control Statements

if age > 18 {
    fmt.Println("Adult")
} else {
    fmt.Println("Minor")
}
  

No parentheses needed in if conditions! Just curly braces.

๐Ÿ” 6. Looping with for

for i := 1; i <= 5; i++ {
    fmt.Println("Count:", i)
}
  

Note: Go has only one loop keyword: for. It replaces while too.

๐Ÿ”š 7. Ending a Program

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!

๐Ÿ’ก Try This Yourself!

Head to Go Playground and try writing a program that:

  • Declares your name and age
  • Prints a message based on age using if
  • Uses a loop to print your name 3 times

๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review