Week 1: Introduction and Basics (Days 1–7)
Focus: Basic Syntax, Data Types, Functions, and Control Flow
Day 1: Introduction to Go
1. Install Go on Your Machine
- Visit the official Go website: https://golang.org/dl/.
- Download the Go installer for your operating system (Windows, macOS, or Linux).
- Follow the installation instructions for your platform.
- After installation, verify by running the command:
go version
2. Understand the Go Programming Language
- Go is an open-source language created by Google. It is designed for simplicity, efficiency, and concurrency.
- Key Features:
- Static Typing: Types are checked at compile time, reducing runtime errors.
- Concurrency: Go has built-in support for lightweight concurrent programming using goroutines and channels.
- Simplicity: Go aims to simplify code and eliminate unnecessary complexity.
3. Set Up Your Go Workspace
- Go uses the workspace model for project organization.
- Create a directory where you’ll keep your Go code (e.g.,
~/go
on Linux/macOS orC:\GoWorkspace
on Windows). - Inside your workspace, create the following structure:
bash ~/go ├── src ├── bin └── pkg
4. Go Toolchain
- The Go toolchain includes tools for:
- go run: Run Go programs.
- go build: Build Go programs into binaries.
- go fmt: Automatically format Go code.
5. First “Hello, World” Program
Create a file called main.go
:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Run the program:
go run main.go
Output:
Hello, World!
Day 2: Variables and Data Types
1. Basic Data Types
- Go supports several basic data types:
- int: Integer type (e.g.,
5
,-10
). - float64: Floating-point type (e.g.,
3.14
,-2.71
). - bool: Boolean type (e.g.,
true
,false
). - string: String type (e.g.,
"Hello"
,"Go"
).
Example:
package main
import "fmt"
func main() {
var x int = 10
var y float64 = 3.14
var isGoFun bool = true
var name string = "Go Language"
fmt.Println(x, y, isGoFun, name)
}
2. Variable Declaration
- Using
var
: Explicit declaration of variables.
var age int = 30
- Shorthand Declaration: The
:=
syntax is used to infer the type.
age := 30
3. Constants
- Constants are immutable values.
const pi = 3.14159
Day 3: Functions and Methods
1. Declaring and Calling Functions
- A function in Go is declared using the
func
keyword.
package main
import "fmt"
func greet(name string) {
fmt.Println("Hello, " + name)
}
func main() {
greet("John")
}
Output:
Hello, John
2. Function Parameters and Return Types
- Go supports functions with multiple return values.
package main
import "fmt"
func add(a, b int) (int, int) {
return a + b, a - b
}
func main() {
sum, difference := add(10, 5)
fmt.Println("Sum:", sum, "Difference:", difference)
}
Output:
Sum: 15 Difference: 5
3. Difference Between Functions and Methods
- Functions are independent, while methods are attached to types.
package main
import "fmt"
type Person struct {
name string
}
func (p Person) greet() {
fmt.Println("Hello, " + p.name)
}
func main() {
person := Person{name: "Alice"}
person.greet()
}
Output:
Hello, Alice
Day 4: Control Flow (If, Else, Switch)
1. If-Else Statements
- Basic conditional statements:
package main
import "fmt"
func main() {
age := 20
if age >= 18 {
fmt.Println("Adult")
} else {
fmt.Println("Not an adult")
}
}
Output:
Adult
2. Switch Statements
- The
switch
statement is a cleaner alternative to multipleif-else
statements.
package main
import "fmt"
func main() {
day := 3
switch day {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
case 3:
fmt.Println("Wednesday")
default:
fmt.Println("Invalid day")
}
}
Output:
Wednesday
Day 5: Loops (for)
1. Traditional for
Loop
- The traditional
for
loop is similar to C/C++ loops.
package main
import "fmt"
func main() {
for i := 1; i <= 5; i++ {
fmt.Println(i)
}
}
Output:
1
2
3
4
5
2. Range-Based for
Loop
for
withrange
is used to iterate over arrays, slices, and maps.
package main
import "fmt"
func main() {
numbers := []int{1, 2, 3, 4, 5}
for index, value := range numbers {
fmt.Println(index, value)
}
}
Output:
0 1
1 2
2 3
3 4
4 5
3. Break and Continue
break
exits the loop,continue
skips the current iteration.
package main
import "fmt"
func main() {
for i := 1; i <= 5; i++ {
if i == 3 {
continue
}
fmt.Println(i)
}
}
Output:
1
2
4
5
Day 6: Arrays and Slices
1. Arrays
- Arrays have a fixed size.
package main
import "fmt"
func main() {
var numbers [5]int
numbers[0] = 1
numbers[1] = 2
fmt.Println(numbers)
}
Output:
[1 2 0 0 0]
2. Slices
- Slices are dynamic and more flexible than arrays.
package main
import "fmt"
func main() {
numbers := []int{1, 2, 3}
numbers = append(numbers, 4, 5)
fmt.Println(numbers)
}
Output:
[1 2 3 4 5]
Day 7: Structs and Pointers
1. Structs
- Structs are used to group data together.
package main
import "fmt"
type Person struct {
name string
age int
}
func main() {
p := Person{name: "Alice", age: 30}
fmt.Println(p)
}
Output:
{Alice 30}
2. Pointers
- Pointers store memory addresses of variables.
package main
import "fmt"
func main() {
x := 10
p := &x
fmt.Println("Address:", p)
fmt.Println("Value:", *p)
}
Output:
Address: 0x... (memory address)
Value: 10
By the end of Week 1, you’ll have learned the fundamental aspects of Go programming, including syntax, data types, functions, control flow, loops, arrays, slices, structs, and pointers. This will set you up for more advanced topics in the coming weeks!
Leave a Reply