Skip to content

Commit 35f474c

Browse files
committed
add
1 parent d73eb9e commit 35f474c

File tree

12 files changed

+4208
-596
lines changed

12 files changed

+4208
-596
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: "Introduction to Go"
3+
description: "Learn about the Go programming language, its features, and why it's worth learning."
4+
order: 1
5+
---
6+
7+
# Introduction to Go
8+
9+
Go, often referred to as Golang, is a modern, statically-typed programming language designed for simplicity, efficiency, and reliability. Created by engineers at Google, it has gained significant popularity for its exceptional performance and ease of use.
10+
11+
## Why Learn Go?
12+
13+
Go has become increasingly popular for several reasons:
14+
15+
- **Fast Compilation**: Go compiles directly to machine code, resulting in extremely fast build times
16+
- **Garbage Collection**: Automatic memory management without the overhead
17+
- **Concurrency Support**: Built-in concurrency primitives make it easy to write efficient concurrent programs
18+
- **Simple Syntax**: Clean, minimal syntax that's easy to read and write
19+
- **Strong Standard Library**: Rich built-in functionality for common tasks
20+
- **Cross-Platform**: Compile for different operating systems and architectures
21+
22+
## Key Features of Go
23+
24+
Go was designed with a few key principles in mind:
25+
26+
### Simplicity
27+
28+
Go's syntax is deliberately kept simple and clean. The language was designed to be easy to learn and read, with a minimal set of keywords and constructs. This makes Go code highly maintainable and accessible to newcomers.
29+
30+
### Performance
31+
32+
Go programs compile to native machine code, offering performance close to C/C++ while providing memory safety and garbage collection. Its compilation speed is remarkably fast, enabling quick development cycles.
33+
34+
### Concurrency
35+
36+
One of Go's standout features is its built-in support for concurrency through goroutines and channels. Goroutines are lightweight threads managed by the Go runtime, allowing you to run thousands of concurrent processes efficiently.
37+
38+
### Standard Library
39+
40+
Go comes with a comprehensive standard library that covers everything from HTTP servers to cryptography, making it possible to build complete applications without external dependencies.
41+
42+
## Getting Started
43+
44+
In the next modules, you'll learn how to set up your Go environment, write your first Go program, and understand fundamental concepts like variables, functions, and data structures.
45+
46+
Let's embark on your Go programming journey!
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: "Hello World in Go"
3+
description: "Write and understand your first Go program"
4+
order: 2
5+
---
6+
7+
# Hello World in Go
8+
9+
Let's start with the traditional "Hello World" program. This simple program will help you understand the basic structure of a Go application.
10+
11+
## Your First Go Program
12+
13+
```go
14+
package main
15+
16+
import "fmt"
17+
18+
func main() {
19+
fmt.Println("Hello World")
20+
}
21+
```
22+
23+
Let's break down this program:
24+
25+
- `package main`: Every Go file starts with a package declaration. The `main` package is special - it defines an executable program rather than a library.
26+
- `import "fmt"`: This imports the format package from the standard library, which provides formatting functions.
27+
- `func main()`: The entry point of the program. Execution begins in the `main` function.
28+
- `fmt.Println("Hello World")`: This prints the text "Hello World" to the console.
29+
30+
## Running Your First Program
31+
32+
To run this program, save it as `hello.go` and execute the following command:
33+
34+
```bash
35+
go run hello.go
36+
```
37+
38+
You should see `Hello World` printed to your console.
39+
40+
## Building an Executable
41+
42+
You can also compile your program into a standalone executable:
43+
44+
```bash
45+
go build hello.go
46+
```
47+
48+
This will create an executable file named `hello` (or `hello.exe` on Windows). You can run this executable directly:
49+
50+
```bash
51+
./hello
52+
```
53+
54+
## Understanding Go Program Structure
55+
56+
Every Go program follows a similar structure:
57+
58+
1. **Package Declaration**: All Go files begin with `package <name>`. Executable programs use `package main`.
59+
2. **Import Statements**: Import other packages that your code needs.
60+
3. **Functions and Variables**: Declare functions and variables. The `main()` function is required for executable programs.
61+
4. **Code Comments**: Add documentation to your code using `//` for single-line comments or `/* */` for multi-line comments.
62+
63+
## Exercise
64+
65+
Try modifying the "Hello World" program to:
66+
67+
1. Print your name instead of "Hello World"
68+
2. Print multiple lines using multiple `fmt.Println()` calls
69+
3. Add comments to explain what the program does
70+
71+
In the next module, we'll explore Go's variables and data types.
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
title: "Variables and Data Types"
3+
description: "Learn about variables, data types, and how to work with them in Go"
4+
order: 3
5+
---
6+
7+
# Variables and Data Types in Go
8+
9+
Go is a statically typed language, which means variable types are checked at compile time. In this module, we'll explore Go's type system and how to work with variables.
10+
11+
## Basic Types in Go
12+
13+
Go has several built-in types:
14+
15+
- **Numeric types**:
16+
- Integers: `int`, `int8`, `int16`, `int32`, `int64`, `uint`, `uint8`, `uint16`, `uint32`, `uint64`
17+
- Floating point: `float32`, `float64`
18+
- Complex: `complex64`, `complex128`
19+
- **Boolean type**: `bool` (values: `true` and `false`)
20+
- **String type**: `string` (UTF-8 encoded text)
21+
- **Error type**: `error`
22+
23+
## Variable Declaration
24+
25+
There are several ways to declare variables in Go:
26+
27+
### Method 1: Explicit Type
28+
29+
```go
30+
var name string = "Gopher"
31+
var age int = 25
32+
var isActive bool = true
33+
```
34+
35+
### Method 2: Type Inference
36+
37+
Go can infer the type from the value:
38+
39+
```go
40+
var name = "Gopher" // string type is inferred
41+
var age = 25 // int type is inferred
42+
var isActive = true // bool type is inferred
43+
```
44+
45+
### Method 3: Short Variable Declaration
46+
47+
Inside functions, you can use the short declaration operator (`:=`):
48+
49+
```go
50+
name := "Gopher"
51+
age := 25
52+
isActive := true
53+
```
54+
55+
This is the most common form used inside functions.
56+
57+
## Multiple Variable Declaration
58+
59+
You can declare multiple variables at once:
60+
61+
```go
62+
var x, y, z int = 10, 20, 30
63+
64+
// Or with type inference
65+
var a, b, c = "hello", 50, true
66+
67+
// Or with short declaration
68+
firstName, lastName, age := "Go", "pher", 25
69+
```
70+
71+
## Zero Values
72+
73+
Variables declared without an explicit initial value are given their zero value:
74+
75+
```go
76+
var i int // Zero value: 0
77+
var f float64 // Zero value: 0.0
78+
var b bool // Zero value: false
79+
var s string // Zero value: "" (empty string)
80+
```
81+
82+
## Constants
83+
84+
Constants are declared using the `const` keyword:
85+
86+
```go
87+
const Pi = 3.14159
88+
const (
89+
StatusOK = 200
90+
StatusNotFound = 404
91+
)
92+
```
93+
94+
Constants can be character, string, boolean, or numeric values.
95+
96+
## Type Conversion
97+
98+
Go doesn't automatically convert between types. You must explicitly convert:
99+
100+
```go
101+
var i int = 42
102+
var f float64 = float64(i)
103+
var u uint = uint(f)
104+
```
105+
106+
## Example: Working with Different Types
107+
108+
```go
109+
package main
110+
111+
import "fmt"
112+
113+
func main() {
114+
// Different ways to declare variables
115+
var a int = 10
116+
var b = 20
117+
c := 30
118+
119+
// Zero values
120+
var d int
121+
var e string
122+
var f bool
123+
124+
// Constants
125+
const Pi = 3.14
126+
127+
fmt.Println("a:", a)
128+
fmt.Println("b:", b)
129+
fmt.Println("c:", c)
130+
fmt.Println("d (zero value):", d)
131+
fmt.Println("e (zero value):", e)
132+
fmt.Println("f (zero value):", f)
133+
fmt.Println("Pi:", Pi)
134+
}
135+
```
136+
137+
## Type Checking in Action
138+
139+
Go's type system helps catch errors at compile time:
140+
141+
```go
142+
x := 10
143+
y := "hello"
144+
z := x + y // This will cause a compile-time error
145+
```
146+
147+
The above code won't compile because Go doesn't allow adding an integer and a string.
148+
149+
## Exercise
150+
151+
1. Create a program that declares variables of different types
152+
2. Try converting between different types
153+
3. Experiment with zero values and constants
154+
155+
In the next module, we'll look at control structures in Go (if statements, loops, and switches).

0 commit comments

Comments
 (0)