Go Syntax

Go Tutorial

Go (Golang) – Basic Syntax

Go syntax is clean, simple, and strict, which helps you write readable and error-free code. Below are the core syntax rules every beginner must know.


 Structure of a Go Program


 

Explanation

  • package main → Entry package

  • import "fmt" → Import standard library

  • func main() → Program starts here

  • {} → Code blocks

  • No semicolon needed (; optional)


 Comments


 


 Variables

Using var

Type Inference

var city = "Delhi"

Short Declaration (:=)

score := 90

Only allowed inside functions


Data Types

Basic Types

int, int64
float32, float64
string
bool

Example


 Constants


Input & Output

Output

Input


 Operators

Arithmetic

+, -, *, /, %

Comparison

==, !=, >, <, >=, <=

Logical

&&, ||, !

 Conditional Statements

if-else

if with short statement


 Loops (Only for loop in Go)

Traditional loop

While-style

Infinite loop


 Functions

Multiple Return Values


 Arrays


Slices (More Common)


Maps


 Structs


 

u := User{Name: “Sanjit”, Age: 25}


 Pointers


 Packages


Summary

  •  Simple
  •  Strong typing
  •  Built-in concurrency support
  •  Ideal for backend & system programming

You may also like...