Fall of the Queensland Koala Data Viz

Fall of the Queensland Koala data viz

A diverse abundance of wildlife is as quintessentially Australian as Crocodile Dundee’s giant knife or Men at Work telling you to run for cover. During my recent move Down Under from New York, my mother catalogued all the snakes, spiders, sharks and crocodiles that kill for sport, but that isn’t what I mean. Though she’s not entirely off base, I prefer to focus on friendlier Australian icons like the kangaroo and koala.

Upon arrival in Australia, it doesn’t take long to see that kangaroos run rampant. It’s estimated that there are twice as many kangaroos as people in Australia, and they are commonly seen as pests. On the other hand, despite widespread adoration, historically exploited koala populations continue to face a grim trajectory. Take a glance at some of the troubling stats below, and check out the links at the bottom to see how you can help!

 

The post Fall of the Queensland Koala Data Viz appeared first on InterWorks.

InterWorks

A Recap of Data Council Conference 2019

Data Council Conference 2019

Have you ever noticed that when you complete a trip with Uber, your account is updated immediately? I often interact with apps on my phone only to be left confused by how they can stream my data between their source systems and my phone so seamlessly. Interacting with Uber as a consumer showcases the extraordinary engineering these companies use to solve data problems. While these final products are intuitive and simple, they are powered by exorbitant amounts of data, which leaves data engineers like me wondering: “How do they do it, and how can I do it for our clients?”

When we first heard about Data Council and saw that companies like Slack, Uber, Lyft and Netflix were presenting, I had a hunch my questions were going to be answered.

What Is the Data Council Conference?

In April, Dylan Bergey, Brenden Goetz and I traveled to San Francisco to attend the 2019 Data Council Conference. Data Council’s main goal is to create a global community of data professionals, and this week embodied that effort completely. Data Council shares a lot of the same values as InterWorks, and as data engineers, it was invaluable to spend a week listening to our peers discuss how data engineering is changing at organizations that define the problems we solve.

While most enterprises are starting to gather data at massive volume, it is obvious that some of the technology organizations in the Bay Area are running into issues never seen before, and as solutions emerge, InterWorks is afforded the opportunity to learn what’s changing in our field and how that change relates to our clients.

The week was filled with speakers from a variety of industries that highlighted how data is changing the way they work. The conference was organized around numerous tracks you could follow. Day one gave us the opportunity to choose to learn about Data Platforms & Pipelines; Machine & Deep Learning; or Hero Engineering. For day one, I chose to follow the Data Platforms & Pipelines track, getting to hear discussions from Netflix, Lyft, Astronomer and more about Data Discovery, ETL and all things data.

Data Council Conference 2019

Day One: Data Platforms and Pipelines

As day one kicked off, I noticed how our speakers’ approach to data engineering seemed different from our clients’ typical approach. Listening to the style of projects these large tech companies pursue, there was one common theme: open source.

Open-source technology is an exciting space. The concept of using community-driven software to power your analytics workloads provides flexibility and allows you to give back through contributions and support of the system. However, for a lot of our clients, an open-source solution doesn’t make sense. At InterWorks, we aim to provide our clients with a comprehensive solution that can be utilized and iterated upon after we leave.

Exploring Open Source

There are a lot of benefits to open-source projects, such as almost always having the lowest up-front investment, but the skillset required to develop and maintain these platforms can quickly evolve beyond what is available to most companies. You don’t call in InterWorks to build something you’re afraid to touch once we leave.

As a result of the complexity created by these open-source tools, we often suggest that clients without the ability to manage them explore options with a fully managed solution. These tend to be easier to maintain and hand over, as well as give us the opportunity to provide value to the user and the business simultaneously. In the end, a solutions goal looks wildly different depending on what the client needs, but we want to provide a solution that makes life easier, not harder.

Day Two: Data Engineering Roles

The second day’s options consisted of Databases & Tools’ Data Analytics and Building Data Products. I hopped around tracks quite a bit and was able to hear talks about data from nearly every perspective available to a business. We started the day listening to a panel discuss the importance of security in the world of data. The speakers did an amazing job unpacking how technical advancements have moved at a quicker pace than regulation, and in order to continue rapid innovation, we need to learn to work with the regulators more effectively.

As the day progressed, we heard presentations from Looker, Maxime Beauchemin, Slack and Stitch Fix. Slack’s discussion about project management for data projects highlighted the importance of data engineering’s role to an organization. Just to provide some background on Slack’s presentation, they recently did a complete restructure for how their teams are supported by data engineers and are pushing that model across the business with great success.

This talk was particularly interesting to me because when working with clients, we are brought in to perform two roles: be the expert, and make the project succeed. Those two skills are crucial to our success as a consultancy, and learning how Slack successfully manages data projects shed some light on how we can continue to improve in that area.

Staying Current in the Data Industry

Attending conferences like Data Council is what keeps InterWorks in tune with the industry and what our clients need. On any given day, we’re sent all over the world to solve data problems for clients who are doing amazing things with their data. Being able to go and hear about what is on the horizon technically and professionally is crucial to our ability to serve clients effectively. The conference delivered and definitely offered up a few things we can grasp onto that will drive us to be better consultants who engineer solutions our clients love.

The post A Recap of Data Council Conference 2019 appeared first on InterWorks.

InterWorks

ListenData: Python Data Structures

This post explains the data structures used in Python. It is essential to understand the data structures in a programming language. In python, there are many data structures available. They are as follows :

  1. strings
  2. lists
  3. tuples
  4. dictionaries
  5. sets

Python Data Structures

1. Strings

Python String is a sequence of characters.

How to create a string in Python

You can create Python string using a single or double quote.

mystring = “Hello Python3.6”
print(mystring)

Output:

Hello Python3.6

Can I use multiple single or double quotes to define string?

Answer is Yes. See examples below –

Multiple Single Quotes

mystring = ”’Hello Python3.6”’
print(mystring)

Output:

Hello Python3.6

Multiple Double Quotes

mystring = “””Hello Python3.6″””
print(mystring)

Output:

Hello Python3.6


How to include quotes within a string?

mystring = r’Hello”Python”‘
print(mystring)

Output:

Hello"Python"

How to extract Nth letter or word?

You can use the syntax below to get first letter.

mystring = ‘Hi How are you?’
mystring[0]

Output

'H'
mystring[0] refers to first letter as indexing in python starts from 0. Similarly, mystring[1] refers to second letter.
To pull last letter, you can use -1 as index.

mystring[-1]

To get first word

mystring.split(‘ ‘)[0]

Output : Hi

How it works –

1. mystring.split(‘ ‘) tells Python to use space as a delimiter.
Output : [‘Hi’, ‘How’, ‘are’, ‘you?’]

2. mystring.split(‘ ‘)[0] tells Python to pick first word of a string.

2. List

Unlike String, List can contain different types of objects such as integer, float, string etc.

  1. x = [142, 124, 234, 345, 465]
  2. y = [‘A’, ‘C’, ‘E’, ‘M’]
  3. z = [‘AA’, 44, 5.1, ‘KK’]

Get List Item

We can extract list item using Indexes. Index starts from 0 and end with (number of elements-1).

Syntax : list[start : stop : step]

  1. start : refers to starting position.
  2. stop : refers to end position.
  3. step : refers to increment value.

k = [124, 225, 305, 246, 259]
k[0]
k[1]
k[-1]

k[0]
124

k[1]
225

k[-1]
259

Explanation :

k[0] picks first element from list. Negative sign tells Python to search list item from right to left. k[-1] selects the last element from list.

To select multiple elements from a list, you can use the following method :

k[:3] returns [124, 225, 305]
k[0:3] also returns [124, 225, 305]
k[::-1] reverses the whole list and returns [259, 246, 305, 225, 124]

Sort list
sorted(list) function arranges list in ascending order.
sorted(list, reverse=True) function sorts list in descending order.

sorted(k) returns [124, 225, 246, 259, 305]
sorted(k, reverse=True) returns [305, 259, 246, 225, 124]

Add 5 to each element of a list

In the program below, len() function is used to count the number of elements in a list. In this case, it returns 5. With the help of range() function, range(5) returns 0,1,2,3,4.

x = [1, 2, 3, 4, 5]
for i in range(len(x)):
    x[i] = x[i] + 5
print(x)

[6, 7, 8, 9, 10]

It can also be written like this –

for i in range(len(x)):
   x[i] += 5
print(x)

Combine / Join two lists

The ‘+’ operator is concatenating two lists.

X = [1, 2, 3]
Y = [4, 5, 6]
Z = X + Y
print(Z)

[1, 2, 3, 4, 5, 6]
Sum of values of two list

X = [1, 2, 3]
Y = [4, 5, 6]
import numpy as np
Z = np.add(X, Y)
print(Z)

print(Z)
[5 7 9]

Similarly, you can use np.multiply(X, Y) to multiply values of two list.

Repeat List N times

The ‘*’ operator is repeating list N times.

X = [1, 2, 3]
Z = X * 3
print(Z)

[1, 2, 3, 1, 2, 3, 1, 2, 3]

Note : The above two methods also work for string list.

Modify / Replace a list item

Suppose you need to replace third value to a different value.

X = [1, 2, 3]
X[2]=5
print(X)

print(X)
[1, 2, 5]

Add / Remove a list item

We can add a list item by using append method.

X = [‘AA’, ‘BB’, ‘CC’]
X.append(‘DD’)
print(X)

Result : [‘AA’, ‘BB’, ‘CC’, ‘DD’]
Similarly, we can remove a list item by using remove method.

X = [‘AA’, ‘BB’, ‘CC’]
X.remove(‘BB’)
print(X)

Result : [‘AA’, ‘CC’]

3. Tuple

Like list, tuple can also contain mixed data. But tuple cannot be changed or altered once created whereas list can be modified. Another difference is a tuple is created inside parentheses ( ). Whereas, list is created inside square brackets [ ]

Examples

mytuple = (123,223,323)
City = (‘Delhi’,’Mumbai’,’Bangalore’)

Perform for loop on Tuple

for i in City:
    print(i)

Delhi
Mumbai
Bangalore


Tuple cannot be altered

Run the following command and check error

X = (1, 2, 3)
X[2]=5

TypeError: ‘tuple’ object does not support item assignment

4. Dictionary

It works like an address book wherein you can find an address of a person by searching the name. In this example. name of a person is considered as key and address as value. It is important to note that the key must be unique while values may not be. Keys should not be duplicate because if it is a duplicate, you cannot find exact values associated with key. Keys can be of any data type such as strings, numbers, or tuples.

Create a dictionary

It is defined in curly braces {}. Each key is followed by a colon (:) and then values.

teams = {‘Dave’ : ‘team A’,
         ‘Tim’ : ‘team B’,
         ‘Babita’ : ‘team C’,
         ‘Sam’ : ‘team B’,
         ‘Ravi’ : ‘team C’
        }

Extract Keys and Values of Dictionary

teams.keys() returns dict_keys([‘Dave’, ‘Tim’, ‘Babita’, ‘Sam’, ‘Ravi’])
teams.values() returns dict_values([‘team A’, ‘team B’, ‘team C’, ‘team B’, ‘team C’])

Find Values of a particular key

teams[‘Sam’]

Output : ‘team B’

Delete an item

del teams[‘Ravi’]


Add an item

teams[‘Deep’] = ‘team B’

Output :
{‘Babita’: ‘team C’,
 ‘Dave’: ‘team A’,
 ‘Deep’: ‘team B’,
 ‘Sam’: ‘team B’,
 ‘Tim’: ‘team B’}

5. Sets

Sets are unordered collections of simple objects.

X = set([‘A’, ‘B’, ‘C’])

Q. Does ‘A’ exist in set X?

‘A’ in X

Result : True

Q. Does ‘D’ exist in set X?

‘D’ in X

Result : False

Q. How to add ‘D’ in set X?

X.add(‘D’)

Q. How to remove ‘C’ from set X?

X.remove(‘C’)

Q. How to create a copy of set X?

Y = X.copy()

Q. Which items are common in both sets X and Y?

Y & X

Planet Python

Understanding Tableau Prep and Conductor: Connecting to a Data Source with Builder

Understanding Tableau Prep and Conductor: Connecting to a Data Source

Before you begin building a workflow using Tableau Prep, it’s helpful to know a little bit about the data source(s) you need to connect to. Consider the following:

  • What kind of source are you connecting to?
  • How large are the files? Record counts? Rows/columns?
  • Are you familiar with the data structures?
  • Do you know of any data quality issues?
  • How frequently will you be updating the data?

Understanding the Basics of Your Data Sources

Understanding the basic data structure and size, as well as the granularity of different sources, helps you plan the flow in your mind before you get into the detailed challenges that the transformation of the raw data sources poses.

In the example I’m drawing upon for this series, I’m using a version of Superstore data I created, along with public data from the Census Bureau. I’m going to create a workflow that will combine four tables containing annual sales data and a single dimension table that will be joined to provide regional manager names. These will be joined with a population dataset from the Census, enabling us to normalize sales for population for each state that had sales.

Connecting to the Sales Data

The data used in this example comes from two different spreadsheets: one that contains four (4) sales worksheets and one (1) Regional Manager worksheet, and another spreadsheet containing the census data.

Experienced Tableau Desktop users should be familiar with the Superstore dataset. In this spreadsheet, I’ve separated each year’s sale into its own worksheet. This data could have been in a text file or a database:

Superstore data for Tableau Prep

The Census data provides population estimates for each state for corresponding years:

Census data for Tableau Prep

Because the world isn’t perfect, we will have to deal with data quality issues in these files, different aggregations of the data, union different files, join files, pivot the data and re-aggregate the data. There are also inconsistencies within specific fields that will have to be cleaned. The datasets are small, but every kind of data transformation step that Tableau Prep provides will have to be utilized to prepare the data for analysis in Tableau Desktop. We will also create a calculation in Prep to normalize sales by state and year for the population in each state.

That data is not perfectly clean, and some of the structures aren’t right. That’s the real world. We’ll use Tableau Prep to address all of the issues and create a clean dataset for other people to use.

Connecting to the Superstore Sales Data

In this first video, you’ll see how to make a data connection to an initial data source and then add other files to that data source. We’ll make the following connections:

  1. Connect to the four sales tables
  2. Demonstrate a wildcard union
  3. Demonstrate a manual union

Using the Wildcard Union in Tableau Prep

Wildcard unions offer an efficient way to bring together many different tables with similar naming conventions that also have consistent column and row structures. If you’re working with unfamiliar datasets that may have data inconsistencies, I believe creating a union manually gives you more direct control and may make it easier for you to deal with data quality issues that emerge as you join each table.

Using the Manual Union in Tableau Prep

I like using manual unions when I’m working with a new dataset because it’s easier to identify mismatched field-naming conventions. The inconsistent field names (Sales vs. Revenue) didn’t appear until I brought in the 2018 sales data. The visual cues that Tableau Prep provided, and the filtering in the data grid for mismatched fields, made it very easy to find and merge two different fields that were actually both holding sales data.

It was also easy to make minor changes using the Profile Cards for specific fields. I used that to remove the Table Names field, which Builder adds automatically when you union data. I don’t want to see that information in my output from this workflow, so I removed it. In addition, because Row ID is a number, Builder treated it as a number in the profile pane and generated a histogram in that field’s profile card. I wanted to validate that the Row ID field is a unique key for this dataset, so I changed the field to a string, and the profile card was changed in a way that made it easy to see every Row ID is, in fact, a single row of the data.

In the next post in this series, I’ll show you how to add a cleaning step to make additional modifications to fields.

The post Understanding Tableau Prep and Conductor: Connecting to a Data Source with Builder appeared first on InterWorks.

InterWorks

How To Convert Data Types in Go

Introduction

In Go, data types are used to classify one particular type of data, determining the values that you can assign to the type and the operations you can perform on it. When programming, there are times when you will need to convert values between types in order to manipulate values in a different way. For example, you may need to concatenate numeric values with strings, or represent decimal places in numbers that were initialized as integer values. User-generated data is often automatically assigned the string data type, even if it consists of numbers; in order to perform mathematical operations in this input, you would have to convert the string to a numeric data type.

Since Go is a statically typed language, data types are bound to variables rather than values. This means that, if you define a variable as an int, it can only be an int; you can’t assign a string to it without converting the data type of the variable. The static nature of data types in Go places even more importance on learning the ways to convert them.

This tutorial will guide you through converting numbers and strings, as well as provide examples to help familiarize yourself with different use cases.

Converting Number Types

Go has several numeric types to choose from. Primarily they break out into two general types: integers and floating-point numbers.

There are many situations in which you may want to convert between numeric types. Converting between different sizes of numeric types can help optimize performance for specific kinds of system architecture. If you have an integer from another part of your code and want to do division on it, you may want to convert the integer to a float to preserve the precision of the operation. Additionally, working with time durations usually involves integer conversion. To address these situations, Go has built-in type conversions for most numeric types.

Converting Between Integer Types

Go has many integer data types to pick from. When to use one over the other is typically more about performance; however, there will be times when you will need to convert from one integer type to another. For example, Go sometimes automatically generates numeric values as int, which may not match your input value. If your input value were int64, you would not be able to use the int and the int64 numbers in the same mathematical expression until you converted their data types to match.

Assume that you have an int8 and you need to convert it to an int32. You can do this by wrapping it in the int32() type conversion:

var index int8 = 15  var bigIndex int32  bigIndex = int32(index)  fmt.Println(bigIndex) 
Output
15

This code block defines index as an int8 data type and bigIndex as an int32 data type. To store the value of index in bigIndex, it converts the data type to an int32. This is done by wrapping the int32() conversion around the index variable.

To verify your data types, you could use the fmt.Printf statement and the %T verb with the following syntax:

fmt.Printf("index data type:    %T\n", index) fmt.Printf("bigIndex data type: %T\n", bigIndex) 
Output
index data type: int8 bigIndex data type: int32

Since this uses the %T verb, the print statement outputs the type for the variable, and not the actual value of the variable. This way, you can confirm the converted data type.

You can also convert from a larger bit-size integer to a smaller bit-size integer:

var big int64 = 64  var little int8  little = int8(big)  fmt.Println(little) 
Output
64

Keep in mind that when converting integers you could potentially exceed the maximum value of the data type and wraparound:

var big int64 = 129 var little = int8(big) fmt.Println(little) 
Output
-127

A wraparound happens when the value is converted to a data type that is too small to hold it. In the preceding example, the 8-bit data type int8 did not have enough space to hold the 64-bit variable big. Care should always be taken when converting from a larger number data type to a smaller number data type so that you do not truncate the data by accident.

Converting Integers to Floats

Converting integers to floats in Go is similar to converting one integer type to another. You can use the built-in type conversions by wrapping float64() or float32() around the integer you are converting:

var x int64 = 57  var y float64 = float64(x)  fmt.Printf("%.2f\n", y) 
Output
57.00

This code declares a variable x of type int64 and initializes its value to 57.

var x int64 = 57 

Wrapping the float64() conversion around x will convert the value of 57 to a float value of 57.00.

var y float64 = float64(x) 

The %.2f print verb tells fmt.Printf to format the float with two decimals.

You can also use this process on a variable. The following code declares f as equal to 57, and then prints out the new float:

var f float64 = 57 fmt.Printf("%.2f\n", f) 
Output
57.00

By using either float32() or float64(), you can convert integers to floats. Next, you will learn how to convert floats to integers.

Converting Floats to Integers

Go can convert floats to integers, but the program will lose the precision of the float.

Wrapping floats in int(), or one of its architecture-independent data types, works similarly to when you used it to convert from one integer type to another. You can add a floating-point number inside of the parentheses to convert it to an integer:

var f float64 = 390.8 var i int = int(f)  fmt.Printf("f = %.2f\n", f) fmt.Printf("i = %d\n", i) 
Output
f = 390.80 i = 390

This syntax would convert the float 390.8 to the integer 390, dropping the decimal place.

You can also use this with variables. The following code declares b as equal to 125.0 and c as equal to 390.8, then prints them out as integers. Short variable declaration (:=) shortens up the syntax:

b := 125.0 c := 390.8  fmt.Println(int(b)) fmt.Println(int(c)) 
Output
125 390

When converting floats to integers with the int() type, Go cuts off the decimal and remaining numbers of a float to create an integer. Note that, even though you may want to round 390.8 up to 391, Go will not do this through the int() type. Instead, it will drop the decimal.

Numbers Converted Through Division

When dividing integer types in Go the result will also be an integer type, with the modulus, or remainder, dropped:

a := 5 / 2 fmt.Println(a) 
Output
2

If, when dividing, any of the number types are a float, then all of the types will automatically be declared as a float:

    a := 5.0 / 2     fmt.Println(a) 
Output
2.5

This divides the float 5.0 by the integer 2, and the answer 2.5 is a float that retains the decimal precision.

In this section, you have converted between different number data types, including differing sizes of integers and floating-point numbers. Next, you will learn how to convert between numbers and strings.

Converting with Strings

A string is a sequence of one or more characters (letters, numbers, or symbols). Strings are a common form of data in computer programs, and you may need to convert strings to numbers or numbers to strings fairly often, especially when you are taking in user-generated data.

Converting Numbers to Strings

You can convert numbers to strings by using the strconv.Itoa method from the strconv package in the Go standard libary. If you pass either a number or a variable into the parentheses of the method, that numeric value will be converted into a string value.

First, let’s look at converting integers. To convert the integer 12 to a string value, you can pass 12 into the strconv.Itoa method:

package main  import (     "fmt"     "strconv" )  func main() {     a := strconv.Itoa(12)     fmt.Printf("%q\n", a) } 

When running this program, you’ll receive the following output:

Output
"12"

The quotes around the number 12 signify that the number is no longer an integer but is now a string value.

You used the := assignment operator to both declare a new variable with the name of a and assign the value returned from the strconv.Itoa() function. In this case, you assigned the value 12 to your variable. You also used the %q verb in the fmt.Printf function, which tells the function to quote the string provided.

With variables you can begin to see how practical it can be to convert integers to strings. Say you want to keep track of a user’s daily programming progress and are inputting how many lines of code they write at a time. You would like to show this feedback to the user and will be printing out string and integer values at the same time:

package main  import (     "fmt" )  func main() {     user := "Sammy"     lines := 50      fmt.Println("Congratulations, " + user + "! You just wrote " + lines + " lines of code.") } 

When you run this code, you’ll receive the following error:

Output
invalid operation: ("Congratulations, " + user + "! You just wrote ") + lines (mismatched types string and int)

You’re not able to concatenate strings and integers in Go, so you’ll have to convert the variable lines to be a string value:

package main  import (     "fmt"     "strconv" )  func main() {     user := "Sammy"     lines := 50      fmt.Println("Congratulations, " + user + "! You just wrote " + strconv.Itoa(lines) + " lines of code.") } 

Now, when you run the code, you’ll receive the following output that congratulates your user on their progress:

Output
Congratulations, Sammy! You just wrote 50 lines of code.

If you are looking to convert a float to a string rather than an integer to a string, you follow similar steps and format. When you pass a float into the fmt.Sprint method, from the fmt package in the Go standard library, a string value of the float will be returned. You can use either the float value itself or a variable:

package main  import (     "fmt" )  func main() {     fmt.Println(fmt.Sprint(421.034))      f := 5524.53     fmt.Println(fmt.Sprint(f)) } 
Output
421.034 5524.53

You can test to make sure it’s right by concatenating with a string:

package main  import (     "fmt" )  func main() {     f := 5524.53     fmt.Println("Sammy has " + fmt.Sprint(f) + " points.") } 
Output
Sammy has 5524.53 points.

You can be sure your float was properly converted to a string because the concatenation was performed without error.

Converting Strings to Numbers

Strings can be converted to numbers by using the strconv package in the Go standard library. The strconv package has functions for converting both integer and float number types. This is a very common operation when accepting input from the user. For example, if you had a program that asked for a person’s age, when they type the response in, it is captured as a string. You would then need to convert it to an int to do any math with it.

If your string does not have decimal places, you’ll most likely want to convert it to an integer by using the strconv.Atoi function. If you know you will use the number as a float, you would use strconv.ParseFloat.

Let’s use the example of the user Sammy keeping track of lines of code written each day. You may want to manipulate those values with math to provide more interesting feedback for the user, but those values are currently stored in strings:

package main  import (     "fmt" )  func main() {     lines_yesterday := "50"     lines_today := "108"      lines_more := lines_today - lines_yesterday      fmt.Println(lines_more) } 
Output
invalid operation: lines_today - lines_yesterday (operator - not defined on string)

Because the two numeric values were stored in strings, you received an error. The operand - for subtraction is not a valid operand for two string values.

Modify the code to include the strconv.Atoi() method that will convert the strings to integers, which will allow you to do math with values that were originally strings. Because there is a potential to fail when converting a string to an integer, you have to check for any errors. You can use an if statement to check if your conversion was successful.

package main  import (     "fmt"     "log"     "strconv" )  func main() {     lines_yesterday := "50"     lines_today := "108"      yesterday, err := strconv.Atoi(lines_yesterday)     if err != nil {         log.Fatal(err)     }      today, err := strconv.Atoi(lines_today)     if err != nil {         log.Fatal(err)     }     lines_more := today - yesterday      fmt.Println(lines_more) } 

Because it is possible for a string to not be a number, the strconv.Atoi() method will return both the converted type, as well as a potential error. When converting from lines_yesterday with the strconv.Atoi function, you have to check the err return value to ensure that the value was converted. If the err is not nil, it means that strconv.Atoi was unable to successfully convert the string value to an integer. In this example, you used an if statement to check for the error, and if an error was returned, you used log.Fatal to log the error and exit the program.

When you run the preceding code, you will get:

Output
58

Now try to convert a string that is not a number:

package main  import (     "fmt"     "strconv" )  func main() {     a := "not a number"     b, err := strconv.Atoi(a)     fmt.Println(b)     fmt.Println(err) } 

You will get the following error:

Output
0 strconv.Atoi: parsing "not a number": invalid syntax

Because b was declared, but strconv.Atoi failed to make a conversion, a value was never assigned to b. Notice that b has the value of 0. This is because Go has default values, referred to as zero values in Go. strconv.Atoi provides an error describing why it failed to convert the string as well.

Converting Strings and Bytes

Strings in Go are stored as a slice of bytes. In Go, you can convert between a slice of bytes and a string by wrapping it in the corresponding conversions of []byte() and string():

package main  import (     "fmt" )  func main() {     a := "my string"      b := []byte(a)      c := string(b)      fmt.Println(a)      fmt.Println(b)      fmt.Println(c) } 

Here you have stored a string value in a, then converted it to a slice of bytes b, then converted the slice of bytes back to a string as c. You then print a, b, and c to the screen:

Output
my string [109 121 32 115 116 114 105 110 103] my string

The first line of output is the original string my string. The second line printed out is the byte slice that makes up the original string. The third line shows that the byte slice can be safely converted back into a string and printed back out.

Conclusion

This Go tutorial demonstrated how to convert several of the important native data types to other data types, primarily through built-in methods. Being able to convert data types in Go will allow you to do things like accept user input and do math across different number types. Later on, when you are using Go to write programs that accept data from many different sources like databases and APIs, you will use these conversion methods to ensure you can act on your data. You will also be able to optimize storage by converting data to smaller data types.

If you would like a deeper analysis of data types in Go, check out our Understanding Data Types in Go article.

DigitalOcean Community Tutorials