Update tech_docs/go_getting_started.md
This commit is contained in:
@@ -1,3 +1,156 @@
|
|||||||
|
# Golang Guide
|
||||||
|
|
||||||
|
## Introduction to Golang
|
||||||
|
Golang, or Go, is a statically typed, compiled programming language designed by Google. It emphasizes simplicity, efficiency, and strong concurrency features.
|
||||||
|
|
||||||
|
## Constants, Variables, and Basic Data Types
|
||||||
|
- **Constants**: Declared using the `const` keyword. Constants cannot be changed once declared.
|
||||||
|
```go
|
||||||
|
const Pi = 3.14
|
||||||
|
```
|
||||||
|
- **Variables**: Declared using the `var` keyword or shorthand `:=` syntax.
|
||||||
|
```go
|
||||||
|
var name string = "Go"
|
||||||
|
age := 10
|
||||||
|
```
|
||||||
|
- **Basic Data Types**: Include `int`, `float64`, `string`, `bool`, etc.
|
||||||
|
```go
|
||||||
|
var x int = 42
|
||||||
|
var y float64 = 3.14
|
||||||
|
var active bool = true
|
||||||
|
```
|
||||||
|
|
||||||
|
## Functions and Control Structures
|
||||||
|
- **Functions**: Defined using the `func` keyword.
|
||||||
|
```go
|
||||||
|
func add(a int, b int) int {
|
||||||
|
return a + b
|
||||||
|
}
|
||||||
|
```
|
||||||
|
- **Control Structures**: Include `if`, `else`, `switch`, and `for`.
|
||||||
|
```go
|
||||||
|
if x > 10 {
|
||||||
|
// code
|
||||||
|
} else {
|
||||||
|
// code
|
||||||
|
}
|
||||||
|
|
||||||
|
switch day {
|
||||||
|
case "Monday":
|
||||||
|
// code
|
||||||
|
default:
|
||||||
|
// code
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Arrays, Slices, Maps, and Loops
|
||||||
|
- **Arrays**: Fixed-size collections.
|
||||||
|
```go
|
||||||
|
var arr [5]int
|
||||||
|
arr[0] = 1
|
||||||
|
```
|
||||||
|
- **Slices**: Dynamic-size arrays.
|
||||||
|
```go
|
||||||
|
s := []int{1, 2, 3}
|
||||||
|
```
|
||||||
|
- **Maps**: Key-value pairs.
|
||||||
|
```go
|
||||||
|
m := map[string]int{"foo": 1, "bar": 2}
|
||||||
|
```
|
||||||
|
- **Loops**: `for` loop is the only loop in Go.
|
||||||
|
```go
|
||||||
|
for i := 0; i < 5; i++ {
|
||||||
|
// code
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Strings, Runes, and Bytes
|
||||||
|
- **Strings**: Immutable sequences of bytes.
|
||||||
|
```go
|
||||||
|
str := "Hello, Go!"
|
||||||
|
```
|
||||||
|
- **Runes**: Unicode code points.
|
||||||
|
```go
|
||||||
|
r := 'a'
|
||||||
|
```
|
||||||
|
- **Bytes**: Mutable sequences of bytes.
|
||||||
|
```go
|
||||||
|
b := []byte(str)
|
||||||
|
```
|
||||||
|
|
||||||
|
## Structs and Interfaces
|
||||||
|
- **Structs**: Custom data types.
|
||||||
|
```go
|
||||||
|
type Person struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
```
|
||||||
|
- **Interfaces**: Define method sets.
|
||||||
|
```go
|
||||||
|
type Describer interface {
|
||||||
|
Describe() string
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Pointers
|
||||||
|
- **Pointers**: Hold memory addresses of variables.
|
||||||
|
```go
|
||||||
|
var x int = 42
|
||||||
|
var p *int = &x
|
||||||
|
*p = 21
|
||||||
|
```
|
||||||
|
|
||||||
|
## Goroutines
|
||||||
|
- **Goroutines**: Lightweight threads managed by Go runtime.
|
||||||
|
```go
|
||||||
|
go func() {
|
||||||
|
fmt.Println("Hello, Goroutine!")
|
||||||
|
}()
|
||||||
|
```
|
||||||
|
|
||||||
|
## Channels
|
||||||
|
- **Channels**: Communication pipes between goroutines.
|
||||||
|
```go
|
||||||
|
ch := make(chan int)
|
||||||
|
go func() {
|
||||||
|
ch <- 42
|
||||||
|
}()
|
||||||
|
val := <-ch
|
||||||
|
```
|
||||||
|
|
||||||
|
## Generics
|
||||||
|
- **Generics**: Type parameters for functions and types (introduced in Go 1.18).
|
||||||
|
```go
|
||||||
|
func Print[T any](x T) {
|
||||||
|
fmt.Println(x)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Building an API
|
||||||
|
- **Building an API**: Use the `net/http` package.
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func handler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Fprintf(w, "Hello, API!")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", handler)
|
||||||
|
http.ListenAndServe(":8080", nil)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This guide provides a foundational overview of key concepts in Go, enabling you to get started with the language and understand its core features.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
Here are detailed and context-rich project ideas focusing on raw performance, leveraging Go's strengths in concurrency, memory management, and efficient execution:
|
Here are detailed and context-rich project ideas focusing on raw performance, leveraging Go's strengths in concurrency, memory management, and efficient execution:
|
||||||
|
|
||||||
### 1. High-Performance RESTful API
|
### 1. High-Performance RESTful API
|
||||||
|
|||||||
Reference in New Issue
Block a user