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

Go Student Notes

Uploaded by

Jonathan J
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Go Student Notes

Uploaded by

Jonathan J
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Go (Golang) Student Notes

Go, also known as Golang, is an open-source programming language designed for simplicity,
reliability, and efficiency. It is statically typed and compiled, making it suitable for building
scalable and high-performance applications.

Table of Contents
1. Basics of Go

2. Variables and Data Types

3. Functions

4. Control Structures

5. Structs and Interfaces

6. Packages and Modules

7. Concurrency in Go

8. Error Handling

9. Testing and Debugging

1. Basics of Go
Go is a statically typed and compiled language developed by Google. It is designed for
simplicity, concurrency, and performance.

Key Features:

 - Simple syntax
 - Built-in garbage collection
 - Strong support for concurrency

2. Variables and Data Types


Variables are declared using the `var` keyword or shorthand `:=`.

Data Types in Go:

 - Basic: int, float64, string, bool


 - Composite: arrays, slices, maps, structs

3. Functions
Functions in Go are defined using the `func` keyword.

Example:
```go
func greet(name string) string {
return "Hello, " + name
}
```

4. Control Structures
Control structures include:

 - Conditional Statements: if, else if, else, switch


 - Loops: for (Go has no while or do-while)

5. Structs and Interfaces


Structs are used to group data together.

Interfaces define a set of methods that types can implement.

Example Struct:

```go
type Student struct {
Name string
Age int
}
```

Example Interface:

```go
type Greeter interface {
Greet() string
}
```

6. Packages and Modules


Go code is organized into packages. Modules are collections of related packages.

Example of Importing a Package:

```go
import "fmt"
fmt.Println("Hello, World!")
```

7. Concurrency in Go
Go supports concurrency using goroutines and channels.
Example Goroutine:

```go
go func() {
fmt.Println("Hello from goroutine")
}()
```

8. Error Handling
Errors in Go are handled using the `error` type.

Example:

```go
func divide(a, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf("division by zero")
}
return a / b, nil
}
```

9. Testing and Debugging


Go has a built-in testing package.

Example Test:

```go
func TestAdd(t *testing.T) {
result := Add(2, 3)
if result != 5 {
t.Errorf("Expected 5, got %d", result)
}
}
```

You might also like