GoLang Basics: A Complete Guide
Let's dive into Go (Golang)! It's a statically typed, compiled language designed for simplicity and
efficiency, with strong support for concurrency. Here's a step-by-step guide to get you started with
the fundamentals of Go.
1. Installation and Setup
- Install Go from the official website (https://golang.org/dl/).
- Verify the installation:
```bash
go version
```
2. Basic Program Structure
Every Go program starts with a `main` package and a `main` function, which is the entry point of the
application.
```go
package main
import "fmt"
func main() {
fmt.Println("Hello, Go!")
}
```
Run your first program:
```bash
go run main.go
```
3. Variables and Types
Go is statically typed, so variable types are known at compile time.
```go
package main
import "fmt"
func main() {
var x int = 10
y := 20 // Short declaration with type inference
var z string = "GoLang"
fmt.Println(x, y, z)
```
4. Basic Data Types
Go has several built-in types:
- Numbers: `int`, `float64`
- Strings: `string`
- Booleans: `bool`
```go
var a int = 42
var b float64 = 3.14
var c bool = true
var d string = "Go is awesome!"
```
5. Control Structures
- If-Else Statement
```go
if x > 10 {
fmt.Println("x is greater than 10")
} else {
fmt.Println("x is less than or equal to 10")
```
- For Loop
Go only has a `for` loop (no `while` or `do-while`).
```go
for i := 0; i < 5; i++ {
fmt.Println(i)
}
```
- Switch Statement
```go
switch day := 3; day {
case 1:
fmt.Println("Monday")
case 2:
fmt.Println("Tuesday")
default:
fmt.Println("Another day")
```
6. Functions
Go functions are declared with the `func` keyword.
```go
func add(a int, b int) int {
return a + b
func main() {
result := add(2, 3)
fmt.Println(result) // Output: 5
}
```
7. Pointers
Pointers in Go allow you to refer to memory locations.
```go
func main() {
x := 10
p := &x // Get memory address of x
fmt.Println(*p) // Dereference pointer to get value at address
```
8. Structs
Structs are custom data types that group together fields.
```go
type Person struct {
Name string
Age int
func main() {
person := Person{Name: "Alice", Age: 25}
fmt.Println(person.Name) // Access field
}
```
9. Methods
You can define methods on types, including structs.
```go
func (p Person) greet() string {
return "Hello, " + p.Name
func main() {
p := Person{Name: "Bob", Age: 30}
fmt.Println(p.greet()) // Call the method
```
10. Concurrency with Goroutines
Go's concurrency model is one of its most powerful features, using goroutines and channels.
- Goroutines: Lightweight threads managed by the Go runtime.
```go
func sayHello() {
fmt.Println("Hello!")
}
func main() {
go sayHello() // Start a new goroutine
time.Sleep(1 * time.Second) // Wait for the goroutine to finish
```
- Channels: Allow communication between goroutines.
```go
func sum(a, b int, ch chan int) {
ch <- a + b // Send value to channel
func main() {
ch := make(chan int)
go sum(2, 3, ch)
result := <-ch // Receive value from channel
fmt.Println(result)
```
11. Error Handling
Go doesn't have exceptions. Instead, it uses error values.
```go
func divide(a, b int) (int, error) {
if b == 0 {
return 0, fmt.Errorf("cannot divide by zero")
return a / b, nil
func main() {
result, err := divide(10, 0)
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Println("Result:", result)
```
12. Package Management
Go modules manage dependencies. You can import third-party libraries using `go get`.
```bash
go get github.com/gin-gonic/gin # Example: Get the Gin web framework
```
13. Building and Testing
- Build your project:
```bash
go build
```
- Run tests:
```bash
go test
```
14. Documentation
Write comments above your functions and structs, and you can generate documentation using:
```bash
go doc
```
15. Further Learning
- Go by Example: https://gobyexample.com/
- The Go Programming Language (book) by Alan Donovan and Brian Kernighan.
- Effective Go: https://golang.org/doc/effective_go