Go for Beginners — Part 4

In the last Article (Which can be found HERE), I covered Variables and Data Types within Go.

In this Article I’m going to cover one of the main packages pre-built within Go.

Print Method

fmt.Println() allows us to print to the console and view data we are working with. This is useful if we want to display data to the user or if we want to alert them that a certain thing has happened

It Prints whatever data you throw at it, as long as the data is within the brackets ()

It formats stuff stupidly well which is a far cry from Pythons Print()statement. For example;

fmt.Println("Println", "formats", "really well")

Which Prints:

Println formats really well

This is just one of the optimizations that I can see the developers of Go intentionally thought about putting in. As that is one of my main pet peeves with Python

As in the name Println() whenever you use the function, It prints on a New line

So what if you don’t want that formatting for whatever reason?

You’d use Print() which does the same thing... Just without the formatting

fmt.Print("The answer is", ": ")

That would print out:

The answer is: 12

Or how about this;


That would output:


So with Print() you don't get the default formatting you do with Println()

Printf Method

The Printf() Method allows us to concatenate strings which is really useful and can make your code look neater

selection1 := "soup"
selection2 := "salad"
fmt.Printf("Do I want %v or %v?", selection1, selection2)

The %v is our placeholder and is known as a verb, The specific letter informs what fills the placeholder. For more information about verbs click HERE

Sprint + Sprintln Methods

At this stage you just need to understand that returning a value and printing a value are two different things and this Method returns values given to it

Sprint() works the exact same as Print()

Sprintln() works the exact same as Println() in the essence it includes spacings

The only difference is the Sprint() Methods are designed to return the values into a variable

For an example on how you would implement this in some Go code;

variableOne := "bruhMomento"
bruhMoment := fmt.Sprintln("Oh god hes even including variables in this such as",variableOne,"look at him go")

As you guessed the Sprint() methods also come with Sprintf() which again works the exact same but just returns the value

template := "I wish I had a %v."
pet := "puppy"
wish := fmt.Sprintf(template,pet)

Getting User Input with Scan()

Another useful Method in the fmt Package is Scan() which allows us to collect User Input

It collects the input off the user up until the first space

An Example would look like this;

fmt.Println("How are you doing?")var response stringfmt.Scan(&response)fmt.Printf("My response was %v", response)

The reason for the &is because Scan() expects and address, this will be covered in more detail later


In Summary, the built-in fmt Package is incredibly useful and gives us a few tools to work with out of the box.

If you’re genuinely enjoying this series please consider donating HERE



Backend Developer, Probably listening to music and drinking coffee

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kodey Thomas

Backend Developer, Probably listening to music and drinking coffee