![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
Now that you’ve got a taste of how the Go language works, it’s time we take a step back and talk about variables and data types. If you’ve ever worked with any other programming language, you should already be familiar with these concepts. However, if Go is your first language, it’s important that you understand the purpose of both variables and data types and how they function.
Without this understanding of variables and data types, you’ll struggle to get up to speed with Go (or any language for that matter). But don’t worry, neither variables nor data types are complex concepts. In fact, variables are quite simple. As far as data types are concerned, you just have to know the types and how they work.
A typical variable works in key-value pairs like this:
key = value
Pretty straightforward.
In Go you can declare a variable, define its data type, and then give it a value. The syntax looks like this:
var variableName dataType
We declare the variable with var give the variable a name with variableName and define the data type with dataType. Let’s say we’re creating a variable for first names
var fname string
What we’ve done is declare a variable named fname (for first name) as a string. We use string because the first name will be comprised of characters a/A-z/Z (more on data types in a bit).
We can also initialize that variable (if we so choose) with a value in the same line like this:
var fname string = New
Let’s use that in a block of code along with a variable for last name as well. Remember (from our previous tutorials) we have to call the main package with:
package main
Next, we have to import “fmt” from main with the line:
import ("fmt")
Now, we’ll create a function that defines our variables and prints the first and last name. The function looks like this:
func main() {
var fname string = "New"
var lname string = "Stack"
fmt.Println(fname,lname)
}
Our entire app (named vars.go) looks like this:
package main
import ("fmt")
func main() {
var fname string = "New"
var lname string = "Stack"
fmt.Println(fname,lname)
}
We run the app with:
go run vars.go
The output would be:
New Stack
Simple and effective.
But how do you initialize a variable with data inputted from the user? Now that’s a cool trick. We’ll stick with our example above. For this, we’ use the fmt.Scan function from the main package. The first thing we have to do (after calling both main and fmt) is declare our variables (inside our function) with:
var fname string var lname string
Next, we write four lines:
This section looks like:
Finally, we write a line to print it all out like so:
fmt.Println("Your name is:", fname, lname)
The entire code looks like this:
When you run the app, it will ask for a first name, then a last name, and print out both names.
Go has a number of included data types, which are broken into three categories:
The basic types are obvious and are defined like so:
These break down like so:
Next, we have the aggregate types, which could be in the following forms:
The struct aggregate type is a bit more complex because it can contain custom fields. You declare a struct like this:
type Box struct {
X int
Y int
}
You could then initialize those values like this:
b := Box{1,2}
Finally, there are the reference types, such as pointers, which contain the memory address of a variable on which they are based. Pointers use the * character like so:
var p *int
We could then declare something like this:
myInteger := 100
We then create the pointer from the variable like this:
We’re going to dive deeper into these concepts later on, but it’s important you understand the basic types included with Go.
Congratulations on taking yet another step with the Go language. It may not be as simple as Python but it’s not nearly as complicated as C, C++, or similar programming languages.