Go · 929 bytes Raw Blame History
1 package main
2
3 import (
4 "fmt"
5 "strings"
6 )
7
8 // Person struct definition
9 type Person struct {
10 Name string
11 Age int
12 }
13
14 // Method on Person
15 func (p Person) Greet() {
16 fmt.Printf("Hello, I'm %s and I'm %d years old\n", p.Name, p.Age)
17 }
18
19 func fibonacci(n int) int {
20 if n <= 1 {
21 return n
22 }
23 return fibonacci(n-1) + fibonacci(n-2)
24 }
25
26 func main() {
27 // Variable declarations
28 var message string = "Hello, Go!"
29 count := 42
30 pi := 3.14159
31
32 fmt.Println(message)
33
34 // Create a person
35 person := Person{Name: "Bob", Age: 25}
36 person.Greet()
37
38 // Slice and loop
39 numbers := []int{1, 2, 3, 4, 5}
40 for i, num := range numbers {
41 if num%2 == 0 {
42 fmt.Printf("Index %d: %d is even\n", i, num)
43 }
44 }
45
46 // Channel example
47 ch := make(chan string)
48 go func() {
49 ch <- "Goroutine message"
50 }()
51
52 msg := <-ch
53 fmt.Println(msg)
54 }