DEV Community

Romulo Gatto
Romulo Gatto

Posted on

Basic Go Projects for Beginners

Basic Go Projects for Beginners

Introduction

Go (or Golang) is a versatile and powerful programming language that has gained significant popularity in recent years. It combines the simplicity of Python with the speed of C++, making it an excellent choice for both beginner and experienced developers. If you are new to Go, this article will guide you through some basic projects that will help you get started and improve your skills.

1. Hello World

Every programmer's journey starts with the classic "Hello World" program. To begin, create a new file called hello.go using your favorite text editor. Add the following code:

package main

import "fmt"

func main() {
    fmt.Println("Hello, world!")
}
Enter fullscreen mode Exit fullscreen mode

Save the file and open your terminal or command prompt. Navigate to the directory where you saved hello.go and execute the following command:

go run hello.go
Enter fullscreen mode Exit fullscreen mode

You should see "Hello, world!" printed on your screen.

2. Calculator App

Building a simple calculator app is a great way to practice basic arithmetic operations in Go.

Create a new file called calculator.go. Add the following code:

package main

import (
    "fmt"
)

func calculate(num1 float64, operator string, num2 float64) float64 {
    result := 0.0

    switch operator {
        case "+":
            result = num1 + num2

        case "-":
            result = num1 - num2

        case "*":
            result= num1 * num2

        case "/":
            if num2 != 0 {
                result = num1 / nunumberm2

            } else {
                fmt.Println("Cannot divide by zero!")
            }

        default:
            fmt.Println("Invalid operation")
    }

    return result
}

func main() {
    var num1, num2 float64
    var operator string

    fmt.Print("Enter the first number: ")
    fmt.Scanln(&num1)

    fmt.Print("Enter an operator (+,-,*,/): ")
    fmt.Scanln(&operator)

    fmt.Print("Enter the second number: ")
    fmt.Scanln(&num2)

    result := calculate(num1, operator, num2)

    fmt.Printf("Result: %f\n", result)  
}
Enter fullscreen mode Exit fullscreen mode

Save the file and run it using the go run calculator.go command in your terminal. You can now perform basic arithmetic operations by following the prompts.

3. Guessing Game

Let's create a simple guessing game where the program generates a random number between 1 and 100, and you have to guess what that number is.

Create a new file called guessing_game.go and add the following code:

package main

import (
    "fmt"
    "math/rand"
    "time"
)

func main() {
    rand.Seed(time.Now().UnixNano())
    target := rand.Intn(100) + 1

    var guess int
    attempts := 0

    for {
        fmt.Print("Guess a number between 1 and 100: ")
        fmt.Scanf("%d", &guess)

        attempts++

        if guess < target {
            fmt.Println("Too low")
        } else if guess > target {
            fmt.Println("Too high")
        } else {
            break // Correct guess!
        }

        if attempts == 10{
            break;
        }

        if attempts != maxAttempts && attempts!=10{
            continue 
        } else { 
            break 
        }







fmt.Printf("\nTarget was %v ", target);
fmt.Printf("\nYou Guessed it in %v ", attempts);
    }
}
Enter fullscreen mode Exit fullscreen mode

Save the file and run it with go run guessing_game.go. The program will generate a random number, and you can start guessing. It will provide feedback if your guess is too high or too low. Try to guess the correct number within 10 attempts.

Conclusion

These basic Go projects are designed to give beginners a hands-on experience with the language while building something useful. As you continue your journey in learning Go, don't be afraid to explore more complex projects and expand your skills. Happy coding!

Top comments (0)