← Back Home

Guessing Game from Rust book in Go

  1. go
  2. rust
  3. programming
  4. learning

This year I have been finding some time and learning Rust. I am going to write about my learning journey in another post. Today I want to talk about converting Guessing game from Rust book to Go. It was a fun and quick exercise. It’s interesting to see that both programms are almost same in size. Here is Rust code from Book.

use std::io;
use rand::Rng;
use std::cmp::Ordering;

fn main() {
	println!("Guess the number!!");

	let secret_number = rand::thread_rng().gen_range(1..=100);

	loop {
		println!("Please input your guess.");

		let mut guess = String::new();

			.read_line(&mut guess)
			.expect("Failed to read line");

		let guess: u32 = match guess.trim().parse(){
				Ok(num) => num,
				Err(_) => continue,

		println!("You guessed: {guess}");

		match guess.cmp(&secret_number) {
			Ordering::Less => println!("Too small!"),
			Ordering::Greater => println!("Too big!"),
			Ordering::Equal => {
				println!("You win!");

And here is my interpretation in Go.

package main

import (

func main() {
	fmt.Println("Guess the number!!")

	secretNumber := rand.Int63n(100)

	for {
		reader := bufio.NewReader(os.Stdin)
		fmt.Print("Enter text: ")
		text, err := reader.ReadString('\n')
		if err != nil {
		fmt.Println("You guessed: ", text)

		guess, err := strconv.ParseInt(strings.Trim(text, "\n"), 10, 64)
		if err != nil {

		if guess < secretNumber {
			fmt.Println("Too small!!")
		} else if guess > secretNumber {
			fmt.Println("Too big!!")
		} else if guess == secretNumber {
			fmt.Println("You win!")

They both have almost same lines of codes and readable at the same time.