1. Go Syntax Overview
Code Structure
In Go, code is organized into packages, which are containers for related code files.
Every Go file starts with a package declaration and typically imports other packages needed to execute the code.
package main
import "fmt" // Importing the 'fmt' package for formatted I/O
func main() {
fmt.Println("Hello, Go!")
}
package main: This declares that your file is part of themainpackage (used for executable programs).import "fmt": Imports thefmtpackage to use functions likefmt.Println()to output text.func main(): Defines themainfunction, which is where your program starts.
2. Variables in Go
In Go, you can declare variables using the var keyword or use short declaration syntax. Here’s how you do it:
Declaring Variables with var
var name string = "John" // Declares a string variable 'name'
var age int = 30 // Declares an integer variable 'age'
Short Variable Declaration
name := "Jane" // Go infers the type (string) automatically
age := 25 // Go infers the type (int)
Zero Value
Every variable in Go has a zero value. For example:
int: 0string: an empty string""bool:false
var x int // x will be 0
var y string // y will be ""
3. Data Types in Go
Go is a statically typed language, meaning each variable has a specific type that can’t be changed. Here’s an overview of the most common data types:
Basic Types
int: Integer numbers (e.g., 1, 2, 100, -50).float64: Decimal numbers (e.g., 3.14, -0.5, 100.00).string: Text (e.g., “Hello, Go!”).bool: Boolean values (trueorfalse).
Example
var name string = "Go"
var age int = 10
var weight float64 = 70.5
var isActive bool = true
Type Inference
Go can automatically determine the type based on the value:
var city = "New York" // type inferred as string
var distance = 100.5 // type inferred as float64
4. Constants in Go
You can define constants in Go using the const keyword. Constants are values that can’t change during the execution of the program.
const Pi = 3.14
const IsGoFun = true
Constants must be given a value when they are declared.
5. Arrays and Slices in Go
Arrays
An array in Go has a fixed length. Once defined, the size cannot be changed.
var numbers [3]int = [3]int{1, 2, 3} // Array with 3 elements
Slices
A slice is a more flexible and powerful data structure in Go. Unlike arrays, slices can grow and shrink in size.
var fruits []string = []string{"apple", "banana", "cherry"}
fruits = append(fruits, "date") // Adding an element to the slice
6. Control Structures in Go
Go provides the basic control structures like if, for, and switch:
If Statement
if age > 18 {
fmt.Println("Adult")
} else {
fmt.Println("Not an adult")
}
For Loop (only loop in Go)
for i := 0; i < 5; i++ {
fmt.Println(i)
}
Switch Statement
switch day := "Monday"; day {
case "Monday":
fmt.Println("Start of the week")
case "Friday":
fmt.Println("End of the week")
default:
fmt.Println("Mid-week")
}
7. Functions in Go
Functions in Go are declared using the func keyword. They can take arguments and return values.
Function Declaration
func greet(name string) {
fmt.Println("Hello, " + name)
}
func add(a int, b int) int {
return a + b
}
Function Call
greet("Alice") // Calls greet function
result := add(3, 5) // Calls add function and stores the result