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

Go Language Fundamentals

The document provides an overview of Go programming language, covering its installation, basic syntax, control structures, functions, data types, and error handling. It includes code examples for various concepts such as variables, loops, arrays, maps, structs, and pointers. Additionally, it concludes with exercises to reinforce the learned concepts.
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)
0 views

Go Language Fundamentals

The document provides an overview of Go programming language, covering its installation, basic syntax, control structures, functions, data types, and error handling. It includes code examples for various concepts such as variables, loops, arrays, maps, structs, and pointers. Additionally, it concludes with exercises to reinforce the learned concepts.
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. Introduction to Go

Go (or Golang) is an open-source programming language developed by Google. It is statically typed,

compiled, and known for its simplicity, concurrency support, and fast performance.

- Designed for efficiency and readability

- Great for systems programming, cloud applications, and web APIs

Chapter 2 : 2. Installing Go

Download from the official Go website (https://golang.org/dl/).

- On macOS: `brew install go`

- On Ubuntu: `sudo apt install golang`

- Verify with: `go version`

Chapter 3 : 3. Hello, World!

```go

package main

import "fmt"

func main() {

fmt.Println("Hello, World!")

```
Go Language Fundamentals

- `package main`: entry point

- `fmt`: package for formatted I/O

- `func main()`: main function

Chapter 4 : 4. Variables and Types

Go supports basic types: `int`, `float64`, `string`, `bool`.

```go

var name string = "Yeison"

age := 30

const pi = 3.14

```

Chapter 5 : 5. Control Structures

If-Else

```go

if age >= 18 {

fmt.Println("Adult")

} else {

fmt.Println("Minor")

```
Go Language Fundamentals

Switch

```go

switch day := "Monday"; day {

case "Monday": fmt.Println("Start of week")

case "Friday": fmt.Println("Weekend soon")

default: fmt.Println("Midweek")

```

Loops

```go

for i := 0; i < 5; i++ {

fmt.Println(i)

```

Chapter 6 : 6. Functions

```go

func add(a int, b int) int {

return a + b

```

Named return values:


Go Language Fundamentals

```go

func divide(a, b float64) (result float64, ok bool) {

if b == 0 {

return 0, false

return a / b, true

```

Chapter 7 : 7. Arrays and Slices

```go

arr := [3]int{1, 2, 3}

slice := []int{1, 2, 3, 4}

slice = append(slice, 5)

```

Chapter 8 : 8. Maps

```go

scores := map[string]int{"Alice": 90, "Bob": 85}

fmt.Println(scores["Alice"])

```

Chapter 9 : 9. Structs
Go Language Fundamentals

```go

type Person struct {

Name string

Age int

p := Person{"Yeison", 30}

fmt.Println(p.Name)

```

Chapter 10 : 10. Pointers

```go

x := 5

ptr := &x

fmt.Println(*ptr) // 5

```

Chapter 11 : 11. Error Handling

```go

import "errors"

func divide(a, b float64) (float64, error) {

if b == 0 {
Go Language Fundamentals

return 0, errors.New("cannot divide by zero")

return a / b, nil

```

Chapter 12 : 12. Summary Exercises

1. Write a function that checks if a number is even.

2. Create a slice of 5 strings and print them.

3. Define a `Book` struct and print its fields.

4. Create a map of 3 countries and their capitals.

5. Write a function that returns the larger of two integers.

You might also like