Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Go is statically typed, meaning that Go knows what the types of your values are even at compile time, and can tell whether they're being used appropriately.
Go is statically typed, meaning that Go knows what the types of your values are even at compile time, and can tell whether they're being used appropriately.
fmt.Println("hello" + 527) // invalid operation: "hello" + 527 (mismatched types string and int)
This code will print the types of several Go values:
package main
import (
"fmt"
"reflect"
)
func main() {
fmt.Println(reflect.TypeOf(1)) // int
fmt.Println(reflect.TypeOf(1.5)) // float64
fmt.Println(reflect.TypeOf("hello")) // string
fmt.Println(reflect.TypeOf(false)) // bool
fmt.Println(reflect.TypeOf(net.IPv4(127, 0, 0, 1))) // net.IPv4
fmt.Println(reflect.TypeOf(time.Now())) // time.Time
}
Go is statically typed.
0:00
This means that Go knows what the types
of your values are, even at compile time.
0:01
And it can tell whether they're
being used appropriately.
0:05
For example, in this program here,
0:08
we try to add the number
527 onto the string hello.
0:10
And before our program even compiles,
we get an error.
0:14
Saying, cannot convert the string
hello to the type int.
0:18
And, this operation is invalid,
trying to add 527 to the value hello.
0:21
In some languages this
would fail at run time,
0:27
possibly while you demoing
your software to a user.
0:29
In Go, it fails at compile time, so
0:32
your development team knows
about the problem immediately.
0:34
There are many built in types for numbers,
0:38
most of which are used only
in special circumstances.
0:40
You should know they exist, so
you're not surprised by them later.
0:43
So I'll describe them to you briefly, but
0:46
then you can forget all
about most of them.
0:48
Because they're really only
two numeric types you're
0:50
going to use on a regular basis.
0:53
For integers you have int,
int8, int16, int32 and int64.
0:55
The numbers at the end of int8, int16,
etc, are the number of bits, ones and
1:00
zeroes, that are used to
store the number in memory.
1:05
The more bits a number takes up,
the larger its maximum value can be.
1:08
int8 can only hold a maximum value of 127.
1:11
You'll get an error if you try to
treat a larger number as an int8.
1:15
But int64 can hold a maximum
value of over 9 quintillion.
1:18
Of course, using more bits means
using more of your computer's memory.
1:23
But computers have so much RAM these days,
1:26
it's generally not worth it
to use the smaller types,
1:29
since the risk of errors is increased
from using a number that's too large.
1:31
The int types can hold negative or
positive numbers,
1:35
but the uint types can hold
only positive numbers.
1:38
Again, unless you have
a specific reason to use a uint,
1:42
it's probably not worth
the additional effort.
1:45
You can just use a int type instead.
1:47
int and
uint types can only hold whole numbers.
1:50
For floating point numbers,
1:53
that is numbers with a decimal point,
you'll want the float32 or float64 types..
1:54
But as I said, you don't need to
remember most of those numeric types.
2:00
By default, Go treats whole numbers
as being of the int type, which is
2:04
the same as the int64 type, unless you're
on an old 32 bit operating system.
2:08
And floating point numbers
are treated as the float64 type.
2:12
We can confirm this by importing
the reflect package and
2:16
calling its type of function on an integer
and on a floating point number.
2:19
If we run this, it will output int for
the integer and float64 for
2:24
the floating point number.
2:27
If you're using a different numeric type
and you wanna do math or comparisons with
2:29
hard coded numbers, you'll have to convert
your values to int or float64 first.
2:33
So unless you have a particular
reason to do otherwise, you'll find
2:38
it's much more convenient to just use the
int and float64 types in your programs.
2:41
Now let's add a string and a Boolean
value and see what types those are.
2:46
So I'll say, reflect.TypeOf and
we'll add a string, hello.
2:53
And then reflect.TypeOf and
we'll do a Boolean value, false.
2:59
Save that, rerun it.
3:04
And we can see that in addition to
the ints and the float64 from before,
3:06
we have a string, and
a bool value, short for Boolean.
3:10
Those are the most frequently
used built in types.
3:15
It's also possible to
define additional types.
3:17
The Go Standard Library includes several
packages that define types of their own.
3:20
For example, if we were to import
the net and time packages, and
3:24
then create a new net.IPv4 instance,
3:28
or a new time object, and
then print the types of those objects,
3:34
we would get net.IP as
the output as well as time.Time.
3:39
Those are the types of those
values that get created
3:45
You need to sign up for Treehouse in order to download course files.
Sign up