Go: Primitives
In my previous blog, we discussed variables in Go. Here we will discuss the basic data types in Go; boolean, numeric, and text. These data types are also known collectively as primitives and are built into Go’s compiler.
**Some examples and overall structure of this blog come from Free Code Camp’s “Learn Go Programming — Golang Tutorial for Beginners” course on YouTube. **
Boolean
Booleans are common in programming languages and represent a value of either true or false. Booleans can be written a few different ways in Go. Below we can see an example of where we can create a variable with the boolean data type.
package mainimport ( "fmt")func main() { var a bool = true var b bool = false// Variables in Go are assigned 0 values unless you explicitly set them like above. The 0 value for a boolean is false var c bool fmt.Printf("%v, %T\n", a, a) fmt.Printf("%v, %T\n", b, b) fmt.Printf("%v, %T\n", c, c)}Output: true, bool
false, bool
false, bool
Booleans are also used in logic tests. In the example below, we set variable a equal to 7 =7, which is true, and set variable b equal to 4 = 7, which is false.
package mainimport ( "fmt")func main() { a := 7 == 7 b := 4 == 7 fmt.Printf("%v, %T\n", a, a) fmt.Printf("%v, %T\n", b, b)}Output:true, bool
false, bool
Numeric
There are various types to represent numbers in Go. However, we can group them into the following major categories:
- Integers
- Floating point
- Complex Numbers
Integers
If you remember back to math class, integers are whole numbers that do not include a decimal. The same is true for integers in Go. In my previous blog regarding variables, I demonstrated a few ways to create integer variables, like the example shown below.
package mainimport ( "fmt")func main() { var i int = 16 fmt.Printf("%v, %T\n", i, i)}16, int
Integers in Go can be broken down further into two types; signed and unsigned. The int
above is a signed integer, which means that it can have positive and negative values. Unsigned integers differ in that they can only be positive numbers or 0. There are many different signed and unsigned integer types and what separates them is the number ranges they can handle. See below for a summary table of all the different types.
**Note: If your value is outside the number range, you will receive a “constant overflows” error message**
Floating Point
Unlike Integers, floating point numbers include decimals. There are two types of floating point numbers, float32
and float64
, and similar to the int types are used depending on the size/level of precision your application requires. Please note, the default value for numbers with a decimal in Go will initialize as a float64
.
See below for examples of working with integers and floating point numbers in Go.
package mainimport ( "fmt")func main() {// Integers: Example below shows type conversion var a int = 10 var b int8 = 3 fmt.Println(a + int(b)) // Operations available to Integers // Arithmetic e := 7 f := 4 fmt.Println(e + f) fmt.Println(e - f) fmt.Println(e * f) fmt.Println(e / f) fmt.Println(e % f) // % is the modulo operator and shows
the remainder of integer division // Bit Operators (watch video for further explanation -
https://www.youtube.com/watch?v=YS4e4q9oBaU) - ~ 1 hour mark fmt.Println(e & f) fmt.Println(e | f) fmt.Println(e ^ f) fmt.Println(e &^ f) // Bit Shifting Operators (watch video for further
explanation - https://www.youtube.com/watch?v=YS4e4q9oBaU) -
~ 1 hour mark fmt.Println(e << 4) fmt.Println(e >> 4)// Floating point: In the below example n = 13.7e72 is too large for a float32 var n float64 = 3.14 n = 13.7e72 n = 2.1e14 fmt.Printf("%v, %T\n", n, n) // Operations available to Floating Point // Arithmetic c := 9.7 d := 4.8 fmt.Println(c + d) fmt.Println(c - d) fmt.Println(c * d) fmt.Println(c / d)}Output:// Integer Type Conversion
13// Integer Arithmetic
11
3
28
1
3// Integer Bit Operators
4
7
3
3// Integer Bit Shifting
112
0// Floating Point
2.1e+14, float64// Floating Point Arithmetic
14.5
4.8999999999999995
46.559999999999995
2.0208333333333335
Complex Numbers
The last number category we will discuss is complex numbers. Complex Numbers are defined as “… any number that can be written as a+bi, where i is the imaginary unit and a and b are real numbers” (Khan Academy). There are two types of complex numbers we can use in Go complex64
and complex128
. You may be asking yourself why 64 and 128? The reason is that in a complex64
, we are adding together real and imaginary numbers that have a type offloat32
, 32 + 32 = 64, and in complex128
, we are adding together real and imaginary numbers that have a type offloat64
, 64+64 =128. See below for of few examples of how we can use complex numbers.
package mainimport ( "fmt")func main() {// Simple Example var n complex64 = 4i fmt.Printf("%v, %T\n", n, n)// Arithmetic Operations a := 4 + 2i b := 7 + 1.9i fmt.Println(a + b) fmt.Println(a - b) fmt.Println(a * b) fmt.Println(a / b)// Splitting apart Real and Imaginary Parts using built-in functions var c complex64 = 2 + 4i fmt.Printf("%v, %T\n", real(c), real(c)) fmt.Printf("%v, %T\n", imag(c), imag(c))// Make a complex number using complex function. First input in complex function is the real number and the second is the imaginary var m complex128 = complex(2, 4) fmt.Printf("%v, %T\n", m, m)}Output:// Simple Example
(0+4i), complex64// Arithmetic Operations
(11+3.9i)
(-3+0.10000000000000009i)
(24.2+21.6i)
(0.6044478236076791+0.12164987644934423i)// Splitting apart Real and Imaginary Parts
2, float32
4, float32// Using Complex function to make a complex number
(2+4i), complex128
Text
The built-in data type for text in Go is strings. Moreover, Go defines strings as “any UTF-8 character”(freecodecamp.org). For a complete list of UTF-8 characters, see here. We saw strings in my last blog when discussing block variables. However, let’s make it a little simpler, here we’ll create a variable str, set it equal to “Hello, Medium!”, and print its value and type.
package mainimport ( "fmt")
func main() { str := "Hello, Medium!" fmt.Printf("%v, %T\n", str, str)}Output:Hello, Medium!, string
When working with strings, you may need to find the value of a certain letter in a string or combine string variables.
Let’s first take a look at finding the value of a certain letter in a string. In Go, each character in a string has an index with the first character starting at 0 and includes spaces. In our “Hello, Medium!” example, to print the letter “o” we could change our print statement above to fmt.Printf("%v\n", str[4])
. However, this doesn’t give us exactly what we need. In Go, strings are aliases for bytes, and the print statement above will give us the unicode value for “o” which is 111
. So, to output “o” we need to change our print statement to include a string conversion fmt.Printf("%v\n", string(s[4]))
.
To combine strings, we can use what is called concatenation. Which is simply using the addition operator to add the strings together. See below for an example.
package mainimport ( "fmt")func main() { str1 := "This is a" str2 := " " str3 := "string" fmt.Printf("%v\n", str1+str2+str3)}Output:This is a string
Thank you for taking the time to learn more about the primitive data types in Go. Keep an eye out for upcoming Go blogs, where we will continue our exploration of this incredible programming language.
Resources
“Learn Go Programming — Golang Tutorial for Beginners.” YouTube, freecodecamp.org, 20 June 2019, www.youtube.com/watch?v=YS4e4q9oBaU.
Doxsey, Caleb. An Introduction to Programming in Go. CreateSpace, 2012.
“Intro to Complex Numbers (Article).” Khan Academy, Khan Academy, www.khanacademy.org/math/algebra2/x2ec2f6f830c9fb89:complex/x2ec2f6f830c9fb89:complex-num/a/intro-to-complex-numbers.
“Go Type System Overview.” Go Type System Overview — Go 101: an Online Go Programming Book + Knowledge Base, go101.org/article/type-system-overview.html.
“Primitive Data Type.” Wikipedia, Wikimedia Foundation, 3 Jan. 2021, en.wikipedia.org/wiki/Primitive_data_type.