0% found this document useful (0 votes)
2 views

Go Data Structures

The document covers fundamental concepts of the Go programming language, including arrays, slices, maps, structs, pointers, interfaces, and embedded structs. Each chapter provides code examples and key points to illustrate the functionality and usage of these features. Additionally, it includes practice exercises to reinforce learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Go Data Structures

The document covers fundamental concepts of the Go programming language, including arrays, slices, maps, structs, pointers, interfaces, and embedded structs. Each chapter provides code examples and key points to illustrate the functionality and usage of these features. Additionally, it includes practice exercises to reinforce learning.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy