Welcome to the world of Go! Whether you're just getting started or looking to refresh your knowledge, this guide will take you through the basics step by step.
Let's kick things off with the classic "Hello, World!" program. Open your Go file and type:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
This simple program prints the famous greeting to the console.
Go is statically typed, meaning you must declare the type of your variables. Here's an example:
package main
import "fmt"
func main() {
x := 5
var y int = 10
z := x + y
fmt.Printf("Sum: %d\n", z)
}
Learn how to declare variables and perform basic arithmetic operations.
Strings in Go can be manipulated and combined. Explore string operations with this code:
package main
import "fmt"
func main() {
greeting := "Hello"
name := "World"
message := fmt.Sprintf("%s, %s!", greeting, name)
fmt.Println(message)
}
Perform operations on numbers in Go:
package main
import "fmt"
func main() {
a := 5
b := 2
sum := a + b
product := a * b
fmt.Printf("Sum: %d, Product: %d\n", sum, product)
}
Learn how to get user input with Go:
package main
import "fmt"
func main() {
var name string
fmt.Println("Enter your name:")
fmt.Scanln(&name)
fmt.Printf("Hello, %s!\n", name)
}
Explore arrays in Go:
package main
import "fmt"
func main() {
numbers := [5]int{1, 2, 3, 4, 5}
for _, num := range numbers {
fmt.Printf("Number: %d\n", num)
}
}
Define functions and call them:
package main
import "fmt"
func add(x, y int) int {
return x + y
}
func main() {
result := add(3, 5)
fmt.Printf("Sum: %d\n", result)
}
Iterate through a range of numbers:
package main
import "fmt"
func main() {
for i := 1; i <= 5; i++ {
fmt.Printf("Number: %d\n", i)
}
}
Implement a simple while loop:
package main
import "fmt"
func main() {
count := 0
for count < 5 {
fmt.Printf("Count: %d\n", count)
count++
}
}
Handle errors with Go's Result type:
package main
import (
"fmt"
"strconv"
)
func main() {
num, err := strconv.Atoi("42")
if err == nil {
fmt.Printf("Parsed number: %d\n", num)
} else {
fmt.Println("Failed to parse")
}
}
Read content from a file:
package main
import (
"fmt"
"io/ioutil"
)
func main() {
content, err := ioutil.ReadFile("example.txt")
if err == nil {
fmt.Printf("File content: %s\n", content)
} else {
fmt.Println("Failed to read the file")
}
}
Write to a file in Go:
package main
import (
"fmt"
"io/ioutil"
)
func main() {
data := []byte("Hello, Go!")
err := ioutil.WriteFile("output.txt", data, 0644)
if err != nil {
fmt.Println("Failed to write to the file")
}
}
Define a simple struct in Go:
package main
import "fmt"
type Dog struct {
Name string
Age uint8
}
func main() {
myDog := Dog{
Name: "Buddy",
Age: 3,
}
fmt.Printf("Dog's name: %s\n", myDog.Name)
fmt.Printf("Dog's age: %d\n", myDog.Age)
}
While Go doesn't have traditional classes, you can achieve similar functionality with structs and methods:
package main
import "fmt"
type Dog struct {
Name string
Age uint8
}
func NewDog(name string, age uint8) Dog {
return Dog{
Name: name,
Age: age,
}
}
func (d Dog) Bark() {
fmt.Printf("%s says woof!\n", d.Name)
}
func main() {
myDog := NewDog("Buddy", 3)
myDog.Bark()
}
Feel free to explore more of Go's powerful features as you continue your learning journey! Happy coding!
This simple Go project is designed for beginners to practice various language features. The program:
-
User Input:
- Receives the user's name and age through standard input.
-
Structs:
- Defines a
Person
struct to represent user data, including name and age.
- Defines a
-
Basic Math Operations:
- Performs basic mathematical operations (sum, product, and square) on the user's age.
-
Arrays:
- Stores the results of the mathematical operations in an array.
-
File I/O:
- Writes both user information and operation results to a file named
output.txt
.
- Writes both user information and operation results to a file named
-
Functions:
- Utilizes functions to modularize code for better organization and readability.
This project covers key Go concepts such as user input, structs, basic operations, arrays, file I/O, and functions. It serves as a practical hands-on exercise for those learning Go programming.
package main
import (
"fmt"
"os"
"strconv"
"io/ioutil"
)
type Person struct {
Name string
Age uint8
}
func main() {
fmt.Println("Enter your name:")
var name string
fmt.Scanln(&name)
fmt.Println("Enter your age:")
var ageInput string
fmt.Scanln(&ageInput)
age, err := strconv.ParseUint(ageInput, 10, 8)
if err != nil {
fmt.Println("Invalid age. Using 0 as default.")
age = 0
}
user := Person{Name: name, Age: uint8(age)}
resultArray := performOperations(user.Age)
welcomeMessage(user)
writeToFile(user, resultArray)
}
func welcomeMessage(person Person) {
fmt.Printf("Welcome, %s! You are %d years old.\n", person.Name, person.Age)
}
func performOperations(age uint8) []uint8 {
sum := age + 5
product := age * 2
square := age * age
fmt.Printf("Age + 5: %d\n", sum)
fmt.Printf("Age * 2: %d\n", product)
fmt.Printf("Age squared: %d\n", square)
return []uint8{sum, product, square}
}
func writeToFile(person Person, results []uint8) {
file, err := os.Create("output.txt")
if err != nil {
fmt.Println("Failed to create the file")
return
}
defer file.Close()
file.WriteString(fmt.Sprintf("Name: %s\nAge: %d\n\n", person.Name, person.Age))
file.WriteString("Results:\n")
for index, result := range results {
file.WriteString(fmt.Sprintf("Operation %d: %d\n", index+1, result))
}
fmt.Println("The information has been written to the 'output.txt' file.")
}