02 Core Basics
Go ka syntax C jaisa hai par bahut clean hai.
1. Variables (var vs :=) ๐ฆ
Go me variable banane ke do tareeke hain:
// 1. Long Wayvar age int = 25
// 2. Short Way (Type Inference)name := "Aditya" // Go khud samajh jayega ye String haiNote: := sirf function ke andar use hota hai.
2. Data Types ๐ข
int,float64(Decimal),string,bool(True/False).- Go static typed hai, matlab
intvariable me text nahi daal sakte.
3. Control Flow (If-Else) ๐ค
Parentheses () ki zaroorat nahi hai!
age := 20if age >= 18 { fmt.Println("Adult")} else { fmt.Println("Minor")}4. Loops (Sirf For Loop!) ๐
Go me while aur do-while loops NAHI hote. Sirf for hai.
// Standard Loopfor i := 0; i < 5; i++ { fmt.Println(i)}
// While Loop ki tarah use karnaj := 0for j < 5 { fmt.Println(j) j++}
// Range Loop (For Arrays/Slices) ๐fruits := []string{"Apple", "Banana"}for index, value := range fruits { fmt.Printf("%d: %s\n", index, value)}5. Arrays & Strings ๐งต
Arrays (Fixed) vs Slices (Dynamic)
- Array: Size fix hota hai.
var arr [5]int. - Slice: Size change ho sakta hai (Recommended).
arr := []int{1, 2, 3}.
Strings
Go me Strings Immutable (change nahi hoti) aur UTF-8 encoded hoti hain.
name := "Hello"// name[0] = 'h' // โ Error! Change nahi kar sakte.length := len(name) // 56. Switch Case (Conditional) ๐
Go ka switch powerful hai, break lagane ki zaroorat nahi hoti.
day := "Mon"switch day {case "Mon": fmt.Println("Work")case "Sat", "Sun": // Multiple cases fmt.Println("Party")default: fmt.Println("Sleep")}7. Structs (Custom Types) ๐๏ธ
Classes ki jagah Go me Structs hote hain.
type Person struct { Name string Age int}
func main() { p1 := Person{Name: "Rohan", Age: 30} fmt.Println(p1.Name)}