Learn to Code via Tutorials on Repl.it!

← Back to all posts
Golang: Googles C++ Part 2 Basic Variables
JustAWalrus (1187)

Hello wonderful people. Before we start can I please say not to get competitive in the comments!

Anyway we are talking about variables today!

So to define a variable we go:

var <name> <type> = <value>

But we can also do this:

<name> := <value>

In this way Go will infer the type, it is a bit slower but your code is much cleaner.

Lets talk about data types!

Please do keep in mind that there are probably about 50 data types in Go but I will keep it simple with just the basics!

Firstly a string:


var x string = "Hello!"

Please note that there are (")'s around the string.

We could also print a string:


var text_to_print string = "Hello World"


Hello World

We can also combine variable strings with regular strings by doing adding a + sign (As well as with other variables):


var x string = "Hello "
x = x + "World"


Hello World

Also note that we can pass in strings combined with variables into a function (As well as with other variables).


var x string = "Hello "
fmt.Println(x + "World")


Hello World

Okay, after strings we have ints.

We can do this:

var x int = 6

And 6 is now stored in x!

We can add to variables of type integer:

var x int = 6
var y int = 6
var z int = x + y

And it works just fine!

Now we can move on to Booleans

These are True/False Values and can be stored easily too!

var IsOn bool = false

And you can just as easily set it to true:

var IsOn bool = true

Once you have set a variable you can edit its value too!

var IsOn bool = true
var IsOn bool = false

Next, we have floating point numbers or numbers with a decimal point.

var FloatNum float64 = 0.5

Please do also keep in mind when setting a variable you don't have to give it an initial value!

Notice that float64? well that means it is stored within 64 bytes we can also do float32!

var Floating32 float32 = 0.5

Anyway that is it for the BASIC types later on we will cover more complex types!

If you enjoyed leave an upvote!

Part 3: https://repl.it/talk/learn/Golang-Googles-C-Part-3-Logic-Operations/42926

Part 4: https://repl.it/talk/learn/Golang-Googles-C-Part-4-Functions/42944

Part 5: https://repl.it/talk/learn/Golang-Googles-C-Part-5-For-Loops/43033