Working with Go

Guessing Game

An example of the guessing game picking a number between 1-100.

Random umber

The math/rand standard library is used to generate the random number.

Note, you must seed the generator, for random this is easiest done using time.Now().UnixNano(). You can also seed the generator with a known value, such as a user id to get a deterministic random number.


Example Game

Here is the example guessing game

package main

import (

// automatically called on start
func init() {
    // new random seed

func main() {
    // variable to store guess
    var guess int

    // variable to store number of guesses
    var count int

    // pick number to guess, add 1 since Intn gives [0,99]
    num := rand.Intn(100) + 1

    fmt.Println(">> I'm thinking of a number between 1-100 << ")

    // guessing loop
    for {
        // prompt user for guess
        fmt.Print("Guess: ")
        _, err := fmt.Scanf("%d", &guess)
        if err == nil {
            count += 1 // increment guess counter
            if guess > num {
                fmt.Println(" >> Too high ")
            } else if guess < num {
                fmt.Println(" >> Too low ")
            } else {
                fmt.Printf("Correct! It took you %d guesses!\n", count)
        } else { // an error with input
            fmt.Println(">> Please input a number")

Hi, I’m Marcus Kazmierczak

Profile Photo I'm an engineer trying to do my small part to make the web a little better. I believe open source can help empower people, and grow our collective knowledge; moving the world forward. I hope this site is a small contribution to that collective.