Concept

The player will guess a number between 0 and 10. If their guess is correct, they win. Otherwise, the program will give the player a hint to either guess higher or lower depending on the correct number. The player will have three shots at the game; if he guesses incorrectly three times, the game would end.

Golang

Simply put, Go is an open-source programming language that makes it easy to build simple, reliable, and efficient software.

In this piece, you will learn how to make a simple guessing game in Golang.

Getting Started With Golang.

To start with, you will need to install Golang on your computer. If you don’t already have it installed, you can do this from the Golang website.

Once you are done with the installation, open the CMD on Windows, Terminal on Mac and Linux. Then change directory “cd” to the folder you want to store your guessing game in.

Create the directory for your guessing game

mkdir Guess

Move into the new Guess directory

cd Guess

Create a new file named guess. go

touch guess. go

You have now successfully installed ‘go’ and also set up your guessing game file.

Now, open the guess.go file with a text/code editor on your device. You can use VS Code. Windows comes with Notepad pre-installed. Mac OS includes TextEdit. Linux users can use Vim. You can also download other text editors like sublime text or atom.

Now let’s get started with coding

Creating the Guessing game

The instructions for this tutorial will be included in the code itself as comments. In Golang, you make comments with double forward slashes.

Once you are done editing, save the program. Also, you can change whatever you want in your program. For example, you could increase the guessing range of numbers from 10 to 100. You could change the program’s response to the player’s actions in the printLn() functions. You can do whatever you want, the game is yours now.

Running Your Program

Open up the terminal of your VS code or command prompt (Windows/Linux) or the terminal (Mac), depending on the choice of text editor you made above . Ensure you are still in the guessing game directory you created above. If you are not, navigate to the guessing game directory using the command below;

cd

There are two methods which you can use to run your guessing game. Build or Run. Use either of the two.

  1. Build

Type the following command

go build guess.go

You should see a new file in your guess directory(folder) named ‘‘guess’’

Then run

./guess

2. Run

Type the following command

go run guess.go

Regardless of the method you went with above; Your game should be running now. Once your program is running, test it out! Play around with it a few times.


Bonus: If you are using VS code, you can open the IDE from your terminal by typing this simple command

code

Note: you must have installed this in your path. To do this, press CMD + SHIFT + P, type shell command and select Install code command in path.

Tags: , , , , , , , , , , , , , , , , , , , , , , ,