Go Student Notes
Go 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
3. Functions
4. Control Structures
7. Concurrency in Go
8. Error Handling
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
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:
Example Struct:
```go
type Student struct {
Name string
Age int
}
```
Example Interface:
```go
type Greeter interface {
Greet() string
}
```
```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
}
```
Example Test:
```go
func TestAdd(t *testing.T) {
result := Add(2, 3)
if result != 5 {
t.Errorf("Expected 5, got %d", result)
}
}
```