Skip to content

Commit f17607f

Browse files
committed
add
1 parent 2ff687a commit f17607f

File tree

10 files changed

+936
-80
lines changed

10 files changed

+936
-80
lines changed

content/courses/quick-start-with-golang-modules/01-introduction-to-go.mdx

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ Go was designed with the following goals in mind:
1919
- **Fast compilation**: Go compiles quickly, enabling rapid development cycles
2020
- **Modern standard library**: Comprehensive built-in packages for common tasks
2121

22+
<Callout type="info">
23+
Go is being used by major companies like Google, Uber, Dropbox, and Twitch, demonstrating its ability to scale and perform in production environments.
24+
</Callout>
25+
2226
## Key Features of Go
2327

2428
### Static Typing with Type Inference
@@ -31,32 +35,72 @@ message := "Hello, Go!"
3135

3236
// Type is explicitly declared
3337
var count int = 42
38+
39+
// Multiple variable declarations
40+
var (
41+
name string = "Gopher"
42+
age int = 5
43+
isAlive bool = true
44+
)
3445
```
3546

3647
### Concurrency with Goroutines
3748

3849
Go's lightweight threads (goroutines) make concurrent programming accessible:
3950

4051
```go
41-
// Start a concurrent function
42-
go fetchData()
52+
package main
53+
54+
import (
55+
"fmt"
56+
"time"
57+
)
58+
59+
func sayHello(message string) {
60+
fmt.Println(message)
61+
}
62+
63+
func main() {
64+
// Start concurrent functions
65+
go sayHello("Hello from goroutine 1")
66+
go sayHello("Hello from goroutine 2")
67+
68+
// Wait to see the output
69+
time.Sleep(100 * time.Millisecond)
70+
fmt.Println("Main function continues...")
71+
}
4372
```
4473

4574
### Channels for Communication
4675

4776
Channels provide a safe way for goroutines to communicate:
4877

4978
```go
50-
// Create a channel
51-
dataChan := make(chan string)
52-
53-
// Send data to the channel
54-
dataChan <- "Some data"
55-
56-
// Receive data from the channel
57-
receivedData := <-dataChan
79+
package main
80+
81+
import "fmt"
82+
83+
func main() {
84+
// Create a channel
85+
dataChan := make(chan string)
86+
87+
// Send data in a goroutine
88+
go func() {
89+
dataChan <- "Hello from channel!"
90+
}()
91+
92+
// Receive data from the channel
93+
receivedData := <-dataChan
94+
fmt.Println(receivedData) // Prints: Hello from channel!
95+
}
5896
```
5997

98+
<Section>
99+
<h2>Comparing Go with Other Languages</h2>
100+
<p>Unlike languages like Python or JavaScript, Go is compiled rather than interpreted, giving it performance advantages. While C++ may be slightly faster in some cases, Go offers a much simpler learning curve and built-in memory management.</p>
101+
<p>Go strikes an excellent balance between performance, simplicity, and developer productivity.</p>
102+
</Section>
103+
60104
## Getting Started
61105

62106
In the next module, we'll set up our Go environment and write our first Go program. You'll see firsthand how Go's simplicity and power make it an excellent choice for modern software development.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
title: "Advanced Code Highlighting Features"
3+
description: "Learn how to use the enhanced code block features like line highlighting, titles, and more."
4+
order: 102
5+
---
6+
7+
# Advanced Code Highlighting Features
8+
9+
This page demonstrates how to use the advanced features of our code blocks.
10+
11+
## Line Highlighting
12+
13+
You can highlight specific lines in your code to draw attention to them. This is done using the `highlight` attribute:
14+
15+
```go highlight="3,5-7"
16+
package main
17+
18+
import "fmt" // This line is highlighted
19+
20+
func main() {
21+
// These lines are highlighted
22+
message := "Hello, highlighted code!"
23+
fmt.Println(message)
24+
}
25+
```
26+
27+
## Code Block Titles
28+
29+
You can add a title to your code blocks using the `title` attribute:
30+
31+
```go title="hello.go"
32+
package main
33+
34+
import "fmt"
35+
36+
func main() {
37+
fmt.Println("Hello from a titled code block!")
38+
}
39+
```
40+
41+
## Hiding Line Numbers
42+
43+
You can hide line numbers if you prefer a cleaner look using the `showLineNumbers` attribute:
44+
45+
```go showLineNumbers="false"
46+
package main
47+
48+
import "fmt"
49+
50+
func main() {
51+
fmt.Println("No line numbers here!")
52+
}
53+
```
54+
55+
## Combining Features
56+
57+
You can combine multiple features for rich code examples:
58+
59+
```go title="example.go" highlight="7-9"
60+
package main
61+
62+
import (
63+
"fmt"
64+
"time"
65+
)
66+
func calculateFibonacci(n int) int {
67+
// Highlighted implementation
68+
if n <= 1 {
69+
return n
70+
}
71+
return calculateFibonacci(n-1) + calculateFibonacci(n-2)
72+
}
73+
74+
func main() {
75+
start := time.Now()
76+
result := calculateFibonacci(10)
77+
elapsed := time.Since(start)
78+
79+
fmt.Printf("Fibonacci(10) = %d\n", result)
80+
fmt.Printf("Time taken: %s\n", elapsed)
81+
}
82+
```
83+
84+
## Comparison with Other Languages
85+
86+
Go syntax is clean and straightforward compared to other languages:
87+
88+
### Go (Golang)
89+
90+
```go
91+
package main
92+
93+
import "fmt"
94+
95+
type Person struct {
96+
Name string
97+
Age int
98+
}
99+
100+
func main() {
101+
p := Person{Name: "Alice", Age: 30}
102+
fmt.Printf("%s is %d years old\n", p.Name, p.Age)
103+
}
104+
```
105+
106+
### JavaScript
107+
108+
```javascript
109+
class Person {
110+
constructor(name, age) {
111+
this.name = name;
112+
this.age = age;
113+
}
114+
}
115+
116+
const person = new Person('Alice', 30);
117+
console.log(`${person.name} is ${person.age} years old`);
118+
```
119+
120+
### Python
121+
122+
```python
123+
class Person:
124+
def __init__(self, name, age):
125+
self.name = name
126+
self.age = age
127+
128+
person = Person('Alice', 30)
129+
print(f"{person.name} is {person.age} years old")
130+
```
131+
132+
By using these code block features, you can create more engaging and educational Go tutorials that highlight the most important parts of your code examples.
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
---
2+
title: "Code Highlighting Examples"
3+
description: "Learn how to use various code syntax highlighting features in Go examples"
4+
order: 101
5+
---
6+
7+
# Go Code Syntax Highlighting Examples
8+
9+
This page demonstrates the enhanced code highlighting capabilities of GolangMastery.
10+
11+
## Basic Hello World
12+
13+
```go
14+
package main
15+
16+
import "fmt"
17+
18+
func main() {
19+
// This is a comment
20+
fmt.Println("Hello, World!")
21+
}
22+
```
23+
24+
## Functions and Types
25+
26+
```go
27+
package main
28+
29+
import (
30+
"fmt"
31+
"time"
32+
)
33+
34+
// Person represents a human being
35+
type Person struct {
36+
Name string
37+
Age int
38+
Birthday time.Time
39+
}
40+
41+
func (p *Person) Greet() string {
42+
return fmt.Sprintf("Hello, my name is %s and I am %d years old.", p.Name, p.Age)
43+
}
44+
45+
func NewPerson(name string, age int) *Person {
46+
return &Person{
47+
Name: name,
48+
Age: age,
49+
}
50+
}
51+
52+
func main() {
53+
person := NewPerson("John Doe", 30)
54+
fmt.Println(person.Greet())
55+
56+
// Numbers demonstration
57+
const pi = 3.14159
58+
var count int64 = 42
59+
60+
// Boolean values
61+
var isGolangFun bool = true
62+
63+
if isGolangFun {
64+
fmt.Println("Learning Go is fun!")
65+
}
66+
}
67+
```
68+
69+
## Error Handling
70+
71+
```go
72+
package main
73+
74+
import (
75+
"errors"
76+
"fmt"
77+
"os"
78+
)
79+
80+
func readFile(filename string) (string, error) {
81+
if filename == "" {
82+
return "", errors.New("filename cannot be empty")
83+
}
84+
85+
data, err := os.ReadFile(filename)
86+
if err != nil {
87+
return "", fmt.Errorf("failed to read file: %w", err)
88+
}
89+
90+
return string(data), nil
91+
}
92+
93+
func main() {
94+
content, err := readFile("example.txt")
95+
if err != nil {
96+
fmt.Printf("Error: %v\n", err)
97+
return
98+
}
99+
100+
fmt.Println("File contents:", content)
101+
}
102+
```
103+
104+
## Goroutines and Channels
105+
106+
```go
107+
package main
108+
109+
import (
110+
"fmt"
111+
"sync"
112+
"time"
113+
)
114+
115+
func worker(id int, jobs <-chan int, results chan<- int, wg *sync.WaitGroup) {
116+
defer wg.Done()
117+
118+
for j := range jobs {
119+
fmt.Printf("Worker %d started job %d\n", id, j)
120+
time.Sleep(time.Second)
121+
fmt.Printf("Worker %d finished job %d\n", id, j)
122+
results <- j * 2
123+
}
124+
}
125+
126+
func main() {
127+
const numJobs = 5
128+
const numWorkers = 3
129+
130+
jobs := make(chan int, numJobs)
131+
results := make(chan int, numJobs)
132+
133+
// Start workers
134+
var wg sync.WaitGroup
135+
for w := 1; w <= numWorkers; w++ {
136+
wg.Add(1)
137+
go worker(w, jobs, results, &wg)
138+
}
139+
140+
// Send jobs
141+
for j := 1; j <= numJobs; j++ {
142+
jobs <- j
143+
}
144+
close(jobs)
145+
146+
// Wait for all workers to complete
147+
wg.Wait()
148+
close(results)
149+
150+
// Collect results
151+
for r := range results {
152+
fmt.Println("Result:", r)
153+
}
154+
}
155+
```

0 commit comments

Comments
 (0)