Go Language Fundamentals
Chapter 1 : 1. Arrays
- Fixed-size collection of elements of the same type
- Indexed from 0
```go
var arr [3]int = [3]int{1, 2, 3}
fmt.Println(arr[0]) // 1
```
Key Points:
- Length is fixed
- Values are initialized to zero values
Chapter 2 : 2. Slices
- Dynamic, flexible view into the elements of an array
```go
nums := []int{10, 20, 30}
nums = append(nums, 40)
fmt.Println(nums)
```
Built-in Functions:
Go Language Fundamentals
- `len(slice)` returns length
- `cap(slice)` returns capacity
- `append(slice, elems...)` adds elements
Slicing:
```go
part := nums[1:3] // [20, 30]
```
Chapter 3 : 3. Maps
- Collection of key-value pairs
```go
capitals := map[string]string{
"France": "Paris",
"Italy": "Rome",
fmt.Println(capitals["Italy"])
```
Map Functions:
```go
capitals["Germany"] = "Berlin"
delete(capitals, "France")
Go Language Fundamentals
value, exists := capitals["Spain"]
```
Chapter 4 : 4. Structs
- Custom data types that group fields
```go
type Book struct {
Title string
Author string
Pages int
myBook := Book{"Go 101", "Yeison", 200}
fmt.Println(myBook.Title)
```
Struct with Methods:
```go
func (b Book) Summary() string {
return b.Title + " by " + b.Author
```
Go Language Fundamentals
Chapter 5 : 5. Pointers with Structs
- Use `&` to create a pointer and `*` to dereference
```go
p := &myBook
p.Pages = 250
fmt.Println(myBook.Pages) // 250
```
Chapter 6 : 6. Interfaces
- Define method sets that a type must implement
```go
type Shape interface {
Area() float64
type Circle struct {
Radius float64
func (c Circle) Area() float64 {
return 3.14 * c.Radius * c.Radius
Go Language Fundamentals
func printArea(s Shape) {
fmt.Println(s.Area())
```
Chapter 7 : 7. Embedded Structs
- Simulate inheritance
```go
type Animal struct {
Name string
type Dog struct {
Animal
Breed string
```
Chapter 8 : 8. Practice Exercises
1. Create a slice of integers and find the average.
Go Language Fundamentals
2. Build a map of countries and their populations.
3. Define a `Movie` struct and print its fields.
4. Implement a `Rectangle` struct with an `Area()` method.
5. Use an interface `Printable` with a `Print()` method.