VOOZH about

URL: https://thenewstack.io/golang-variables-and-data-types-an-introduction/

⇱ Golang Variables and Data Types: An Introduction - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

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.

What’s next?

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.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-04-04 14:00:29
Golang Variables and Data Types: An Introduction
Data / Go

Golang Variables and Data Types: An Introduction

If you want to program in the Go programming language, you need to learn how variables and data types work. Easy-peasy: Start here.
Apr 4th, 2024 2:00pm by Jack Wallen
👁 Featued image for: Golang Variables and Data Types: An Introduction
Feature image from Eduardo via Pixabay. 

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.

Variables

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:

  1. Instruct the user to type their first name.
  2. Accept input for the first name.
  3. Instruct the user to type their last name.
  4. Accept input for the last name.

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.

Data Types

Go has a number of included data types, which are broken into three categories:

  • Basic (bool, int, float64, complex128, string)
  • Aggregate (array, slice, struct)
  • Reference (pointer, channel, map, interface)

The basic types are obvious and are defined like so:

  • var boolean bool = true
  • var integer int = 100
  • var float float64 = 100.09
  • var comp complex128 = 1 + 3i
  • var st string = “New Stack”

These break down like so:

  • boolean is true/false
  • int is a whole number
  • float64 is a number with a decimal
  • complext128 is the set of all complex numbers with boat float64 and imaginary components
  • string is a string of characters

Next, we have the aggregate types, which could be in the following forms:

  • someArray := [10]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
  • var slice []int = someArray[0:5]

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.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.