Go Card Program

Simple Card game in Go

This program is a just a simple game of cards that will show off some of the features of go and explain how it differs from other languages.

In go we must specify a what package type our program is at the top of the file.

package main

When we specify main as our package will know that this is a program that we will be running or building. If however this program we are writing is just a module we want to import in another program we would specify a name for our module here on the package line. In this case I want to just run this program so it will be my main package.

IMPORTS

The next block we will write in our go file will be out imports. The imports block is used to import library files or modules that we will be using. We can even specify a git url on the imports line and we build our code go will go out and import code from the repo.

Here is an example of the imports block:

import "fmt"

In this simple program we will only be needing the fmt library. The fmt library is a builtin library to go and is used for formatted I/O, or more simply put it is used to print out and take input.

Variables

After the imports block we get to our actual code. Normally this is where you will see functions defined and we will get to that, but variables can also be defined in this section of the file. Variables CAN NOT have a value assigned to them out of a function so that will be important to keep in mind.

With that in mind lets look at some examples of variables being initailized:

// Name of the game we will play
var game string

You will notice that in go variables are created by specifying var. After var we list the name of the variable which in this case is game. Finally after our variable's name we specify what type of variable it is which in this case we went with string. In go we have quite a few variable types that we can use, here is some common variable types that are used in go:

Type Description Examples
bool true or false (true, false, 1, 0)
int whole numbers (1, 6782341, -84256689)
float64 decimal place numbers (2.82734105, -0.45, 1.1)
string letter or string of characters ("a", "Hi", "Hey There!")

More variable types exist and can be found in the docs.

Arrays and Slices

Another type of data type we have available to us in go is both arrays and slices. Arrays are treated the same as they are in most programming languages as in they are a collection of elements of the same type. Arrrays can not be scaled up or shrunk down after they are defined, they have a fixed length. To solve that go introduces slices; a special data type in go. A slice can be pictured as an array that grow and shrink as you require throughout your program. Lets look at an example of each being declared:

// Array of our 5 tables
var tablelist [5]int

// Slice for our deck
var card []string

Functions

Every program in go requires a function named main to signify where the code starts. Functions make up the bulk of where your programs logic will be written. Function's in go are written as func followed by the name of the function. Immediately after the name of the function we list the inputs that this function takes or a blank list to specify no input: main(). After the name of the function and the list of inputs we can list a return type. This is only required if we plan to return something when we call on the function as the default function of a function is to not return anything.

A simple function with no input can be written as follows:

func newCard() string {
        // return a new card name
        return "Five of Diamonds"
}

Take note of the return line. In this example we will be returning a string with a card's name so we specify string right after the name of the function: newCard() string.

Loops

Go gives us the ability to loop using for loops. Unlike most other programming languages the for loop in go is used for all forms of loops. For loops in go have components to them:

the init statement
executed before the first iteration
the condition expression
evaluated before every iteration
the post statement
executed at the end of every iteration
// will run 100 times (0-99)
for i := 0; i < 100; i++ {
	fmt.Println(i)
}

It is possible in go to leave off the init and the post statement:

i := 1

// will run 99 times (1-99)
for i < 100 {
	fmt.Println(i)
	i++
}

This is comparable to a while loop in most other programming languages.

You can also ommit the condition statement and this will create a loop that runs forever:

// will run till the heat death of the universe
for {
	fmt.Println("This will never end!")
}

Custom data types

Similar to how some programming languages have object oriented go has custom data types. With custom data types we can essentially…

package main

import "fmt"

func main() {
        fmt.Println("deck")
}

Lets put it all together

Now that we have covered most of the basic of go lets put together our main:

func main() {
        cards := []string{"Ace of Diamonds", newCard()}
        cards = append(cards, "Six of Spade")

        for index, card := range cards {
                fmt.Println(index, card)
        }

        game="BlackJack"
        fmt.Println(game)

        for i := range tablelist {
                tablelist[i]=i + 1
        }

        fmt.Println(tablelist)
}
This page was last updated: not defined. Source