Go Language Types




Welcome to part 3 of the Go programming tutorial series. In the previous tutorial, we covered some basics on imports and functions. Next, we're going to talk a bit about types in Go. As mentioned in the first tutorial, Go is a static-typed language, meaning you need to specify the type, and the type cannot change without you explicitly changing it.

Let's consider a function that adds some numbers together. To do this, our function will look for 2 numbers, and we know it will return a number. With typing, all variables and parameters need types. If you have a function that returns something, you also need to specify the type. Here's a list of the Go language types:

bool

string

int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64

complex64 complex128

Source: golang.org

We'll use float64 in this case to make a point. In many cases, float32 is a superior choice, especially with a language like Go, which is centered around high performance and scale. Unless you need the high precision of float64, float32 might be a better "default" choice. To begin, let's start our script and build the add function:

package main

import "fmt"


func add(x float64, y float64) float64 {
	return x+y
}

Every variable and parameter needs to have a type defined with it. x float64, y float64 means our x and y parameters will be float64. The float64 outside of the parenthesis and before the curly brace is the type for the return. If there are multiple items in the turn, then you would encase the types of the return in parenthesis after the parameters. Next, we can build our main function:

func main() {
	var num1 float64 = 5.6
	var num2 float64 = 9.5

    fmt.Print(add(num1,num2))
}

Notice how we're defining our variables in the function. The full script up to this point:

package main

import "fmt"


func add(x float64, y float64) float64 {
	return x+y
}

func main() {
	var num1 float64 = 5.6
	var num2 float64 = 9.5

    fmt.Print(add(num1,num2))
}

If we run this: go run whateveryoucalledit.go, you should get 15.1 as the return in your console.

Easy enough, but we can clean this up quite a bit. Any time in programming where you find yourself repeating similar lines, you could probably do it better. For example, in our add function:, we define two parameters which have the same type. If you have a situation like this, you can list out all the same-typed parameters or variables, followed at the very end with their type, like:

func add(x, y float64)

We could do the same thing in our main function as well, with the variables:

func main() {
	var num1, num2 float64 = 5.6, 9.5
    fmt.Print(add(num1,num2))
}

There may be times when you want to prepare some variables, to be populated later. You can also do something like var num1 float64, and then assign it a value later. When you define, if you don't assign a value, strings default to "", booleans default to false and numerical types default to 0.

Finally, within functions, we can actually not assign typing, and just define variables like we might in a dynamically-typed language, using := as the assignment operator. In our case, this would be something like:

func main() {
	num1, num2 := 5.6, 9.5
    fmt.Print(add(num1,num2))
}

"HEY! You said Go was static-typed!" Outside of your functions, you will need to define explicitly data types. Inside your functions, Go can figure out your types when compiled, but the assigned types *cannot* be changed without being explicitly converted. In some cases, this can cause you trouble if you're not careful. For now, our full script is:

package main

import "fmt"


func add(x, y float64) float64 {
	return x+y
}

func main() {
	num1, num2 := 5.6, 9.5
    fmt.Print(add(num1,num2))
}

Cool, but what if we were using float32 instead?

package main

import "fmt"


func add(x, y float32) float32 {
	return x+y
}

func main() {
	num1, num2 := 5.6, 9.5
    fmt.Print(add(num1,num2))
}

Running this returns an error:

# command-line-arguments
.\gotut3-functions-types.go:12: cannot use num1 (type float64) as type float32 in argument to add
.\gotut3-functions-types.go:12: cannot use num2 (type float64) as type float32 in argument to add

This error is telling us that we're attempting to treat a float64 as a float32, and we can't do that. We never assigned float64 to our variables, but this is what Go automatically went with.

Since I just mentioned it, but didn't show it, if you have a function that returns multiple values, you specify their types inside parenthesis like so:

func multiple(a,b string) (string,string) {
	return a,b
}

We could use it like so:

package main

import "fmt"


func multiple(a,b string) (string,string) {
	return a,b
}

func main() {
	w1,w2 := multiple("Hey","there")
	fmt.Println(w1,w2)
}

Note our use of double quotes, not singles. If you use single quotes, you will see: .\yourgofilename.go:11: missing '

In case it's not obvious, you see the filename, the line #, and then the error in the error messages.

If you wanted to convert the types of a variable:

	var a int = 62
	var b float64 = float64(a)

Finally, type inference works in Go:

	var x float32
	y := x // y is float32 type

The next tutorial:





  • Introduction to the Go Programming Language
  • Go Language Syntax
  • Go Language Types
  • Pointers in Go Programming
  • Simple Web App in Go Programming
  • Structs in the Go Programming Language
  • Methods in Go Programming
  • Pointer Receivers in Go Programming
  • More Web Dev in Go Language
  • Acessing the Internet in Go
  • Parsing XML with Go Programming
  • Looping in Go Programming
  • Continuing our Go Web application
  • Mapping in Golang
  • Mapping Golang sitemap data
  • Golang Web App HTML Templating
  • Applying templating to our Golang web app
  • Goroutines - Concurrency in Goprogramming
  • Synchronizing Goroutines - Concurrency in Golang
  • Defer - Golang
  • Panic and Recover in Go Programming
  • Go Channels - Concurrency in Go
  • Go Channels buffering, iteration, and synchronization
  • Adding Concurrency to speed up our Golang Web Application