Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds

Free eBook - The Go Workshop

4.8 (20 reviews total)
By Delio D'Anna , Andrew Hayes , Sam Hennessy and 3 more
  • A new free eBook every day on the latest in tech
  • 30 permanently free eBooks from our core tech library
  1. 1. Variables and Operators
About this book
The Go Workshop will take the pain out of learning the Go programming language (also known as Golang). It is designed to teach you to be productive in building real-world software. Presented in an engaging, hands-on way, this book focuses on the features of Go that are used by professionals in their everyday work. Each concept is broken down, clearly explained, and followed up with activities to test your knowledge and build your practical skills. Your first steps will involve mastering Go syntax, working with variables and operators, and using core and complex types to hold data. Moving ahead, you will build your understanding of programming logic and implement Go algorithms to construct useful functions. As you progress, you'll discover how to handle errors, debug code to troubleshoot your applications, and implement polymorphism using interfaces. The later chapters will then teach you how to manage files, connect to a database, work with HTTP servers and REST APIs, and make use of concurrent programming. Throughout this Workshop, you'll work on a series of mini projects, including a shopping cart, a loan calculator, a working hours tracker, a web page counter, a code checker, and a user authentication system. By the end of this book, you'll have the knowledge and confidence to tackle your own ambitious projects with Go.
Publication date:
December 2019
Publisher
Packt
ISBN
9781838647940

 

2. Logic and Loops

Overview

In this chapter, we'll use branching logic and loops to demonstrate how logic can be controlled and selectively run. With these tools, you'll have control of what you do and don't want to run based on the values of variables.

By the end of this chapter, you will be able to implement branching logic using if, else, and else if; use switch statements to simplify complex branching logic; create looping logic using a for loop; loop over complex data collections using range; and use continue and break to take control of the flow of loops.

 

Introduction

In the previous chapter, we looked at variables and values and how we can temporarily store data in a variable and make changes to that data. We're now going to look at how we can use that data to run logic, or not, selectively. This logic allows you to control how data flows through your software. You can react to and perform different operations based on the values in your variables.

The logic could be for validating your user's inputs. If we were writing code to manage a bank account, and the user asked to withdraw some money, we could check that they asked for a valid amount of money. We would check that they had enough money in their account. If the validation was successful, we would use logic to update their balance, transfer the money, and show a success message. If the validation failed, we'd show a message explaining what went wrong.

If your software is a virtual world, then logic is the physical law of that world. Like the physical laws...

 

Summary

In this chapter, we discussed logic and loops. These are the foundational building blocks to build complex software. They allow you to have data flow through your code. They let you deal with collections of data by letting you execute the same logic on every element of the data.

Being able to define the rules and laws of your code are the starting points of codifying the real world in software. If you are creating banking software and the bank has rules about what you can and can't do with money, then you can also define those rules in your code.

Logic and loops are the essential tools that you'll use to build all your software.

In the next chapter, we'll look at Go's type system and the core types it has available.

About the Authors
  • Delio D'Anna

    Delio D'Anna is an experienced software engineer with a company experience mainly developed working on web applications. Although he has been working mostly under a LAMP stack on the back-end side and with JavaScript on the front end, he is interested in a wide range of languages/technologies and always open to learn/apply what comes new from the IT world. His programming background started with the study of algorithms and artificial intelligence but his working career proceeded in the open source world programming high traffic websites in php/MySQL/Postgres/JavaScript.

    Browse publications by this author
  • Andrew Hayes

    Andrew Hayes has 8+ years of experience as a software engineer, including 3 years working with Go. Andrew has worked in a number of areas within the software industry, some which include storage, networking, DevOps, and cloud. He enjoys sharing knowledge by creating tutorials online and speaking at conferences. In his spare time he enjoys camping and fishing.

    Browse publications by this author
  • Sam Hennessy

    Sam Hennessy started working as a software developer in 2002, switching over to using Go for all his projects in 2015. He's worked at start-ups, agencies, and Fortune 500s. His passion for sharing knowledge leads him to write blogs, books, and present at user groups and conferences. Sam also works as an instructor, teaching Go and other topics. Sam's favorite thing is being at home, watching a good film, with his family and his dog Waffles.

    Browse publications by this author
  • Jeremy Leasor

    Jeremy Leasor has over 20 years of IT experience, including 3 years as a security engineer developing Go libraries for attack surface analysis, stig/fisma data imports, and various other utilities,10 years as a lead database administrator (SQL Server) of mission essential system environments, and 8 years as a developer DBA and .NET developer all within the government domain. He enjoys creating various utilities in Go outside of the office hours. In his free time, he enjoys spending time with the family, camping, coaching his kid's soccer team and getting demolished by his kids in Smash Brothers.

    Browse publications by this author
  • Gobin Sougrakpam

    Gobin Sougrakpam started in IT as a Systems Integration Developer for large scale enterprises. His love for automation see him move into a Software Developer role. He currently works as Golang developer, creating automation solutions for Infrastructure platforms like VMware and AWS. Gobin enjoys playing his guitar in his free time.

    Browse publications by this author
  • Dániel Szabó

    Daniel Erno Szabo is a 28-year-old IT geek. He is currently working as a DevOps engineer for an American company in Hungary. His main profile is automation and custom solutions based on different languages covering Windows and Linux. His favorite language is Python, but he is also fluent in Ruby, PowerShell, and C#. He loves learning, teaching, and sharing his knowledge with other people. In his free time, he is a YouTuber and a ferret owner.

    Browse publications by this author
Latest Reviews (20 reviews total)
Great Book
This book walks you through the basics of Go all the way up to some of the advanced concepts. You will learn the Go programming language and have projects that you have built by the end of this book. The projects are fun the writing style is very easy to understand and helps to solidify the concepts taught.If you are looking for a way to learn Go and learn it well I highly recommend this book.
The book is great and comprehensive for the beginners. It helps you to understand how Go works and how you can use Go for day-to-day use and building your basic projects. Some examples in DB, Network, Security and Concurrency has more advanced content that the reader can learn and use in the real world. I suggest to add more contents about Go in production and larger projects.
Recommended For You
The Kubernetes Workshop

From building your own cluster to running cloud-native applications with Kubernetes, this workshop covers it all using engaging examples and activities

By Zachary Arnold and 5 more
The Python Workshop

Learn the fundamentals of clean, effective Python coding and build the practical skills to tackle your own software development or data science projects

By Andrew Bird and 4 more
The Docker Workshop

Get started with Docker on your local machine and progress towards deploying useful applications in production with this simplified, practical guide

By Vincent Sesto and 4 more
Kubernetes and Docker - An Enterprise Guide

Apply Kubernetes beyond the basics of Kubernetes clusters by implementing IAM using OIDC and Active Directory, Layer 4 load balancing using MetalLB, advanced service integration, security, auditing, and CI/CD

By Scott Surovich and 1 more