From the course: Learning Go

Explore Go's variable types - Go Tutorial

From the course: Learning Go

Explore Go's variable types

- [Instructor] As with all programming languages you can store data in memory in Go using variables. Go is a statically typed language. That means that each variable must be assigned to type and once assigned it can't be changed. You can set types for each variable either explicitly, by naming the type in the variable declaration or implicitly, by allowing the compiler to infer the type based on a value that you initially assign. Go comes with a set of built-in data types. You can also define your own data types but these are the ones that are always available to you. The first type is bool, for Boolean. This is a simple true, false value and the only two values that you can assign are true and false. String types are collections of characters. A variable with the type of string contains a series of characters. We'll be talking about strings a lot. Integers come in a variety of flavors. These are the fixed integer types. They each declare either an unsigned or assigned integer and the numeric value in the name is the number of bits. That affects the range or the highest and lowest values that you can assign. There's also a set of type aliases, the byte, the unsigned int, the int and the unsigned int pointer. The int and unsigned int data types are interesting. They reflect either a 32 or a 64 bit value depending on what operating system you're running on. On macOS and on 64 bit windows these are the same as an int64 and a uint64. But, if you run exactly the same code in the Go playground those become 32 bit values because of the underlying operating system. Here are some other built-in numeric types. There are two floating types, float32 and float64. And, once again, the numeric values in the names indicate the number of bits used for storage and therefore, the range that's available to you. And then there are what are known as the complex types, complex64 and complex128. A complex number contains two parts, real numbers and imaginary numbers. Now, if that sounds complicated don't worry about it we won't be using those in this beginning course, but if you understand the concept of complex numbers these types are available. And then there are also built-in types for data collections. There are Arrays and Slices to manage order data collections and Maps and Structs to manage aggregations of values. There are also types that deal with language organization. These exist in other languages but aren't always considered types in those languages. In Go, a function is a type and that's what makes it possible to take a function and pass it into another function as an argument. Interfaces and Channels are also types in Go's and Go also supports pointers, reference variables that point to an address in memory to refer to another value. These are the built-in types in Go but they're just the beginning point, because again, you can create your own data types in this language.

Contents