Thursday, August 7, 2025

Go Programming Language For Dummies

 Introduction

Today, if you’re a programmer, you have lots of options when it comes to choosing a programming language. Popular programming languages include C++, C#, Go, Java, JavaScript, Python, R, Swift, and many more. Each language is designed to solve a different set of problems and, depending on what you’re going to create (mobile apps, web apps, desktop apps), you may end up learning one or more of these languages

 So, why Go? Turns out that three engineers at Google were frustrated with the various toolsets that they were working on and set out to design a new language that would address the criticisms of other languages while at the same time keep ing their useful features.

 Go was designed to

 » Use static typing and have the run-time efficiency of C

 » Have the readability and usability of languages like Python and JavaScript

 » Exhibit great performance in networking and multiprocessing

 The problems with existing languages forced the team at Google to design a new language from the ground up, creating a lean and mean language designed for massive multithreading and concurrency.

 This book covers the basics of Go (also known as Golang), one of the fastest growing programming languages specifically designed to build faster and more scalable applications.

Hello, Go!

Go is an open-source programming language — one of the fastest-growing programming languages around — released by Google in 2009. It’s a mul tipurpose programming language specifically designed to build fast, scal able applications.

 Go comes from a pretty impressive team of people: Ken Thompson (designer and creator of Unix and C), Rob Pike (cocreator of UTF-8 and Unix format), and Robert Griesemer (a Google engineer). If you’re technically inclined, you may want to check out an article called “Go at Google: Language Design in the Service of Soft ware Engineering” (https://talks.golang.org/2012/splash.article), which discusses how Go was initially conceived to solve problems at Google.

 In this chapter, I explain why learning Go is important for your career, where Go can be used, and how to get started with Go programming.

Go is often referred to as Golang because of its web address: https://golang.org. However, the official name of the language is Go, so that’s how I refer to it throughout this book

Seeing What Learning Go Can Do for You

 You can learn many programming languages today, but Go stands out from the others for a few reasons:

 Go is easy to learn. Go’s syntax makes it a readable language. It has no support for object-oriented programming (OOP), which means you don’t have to worry about classes and inheritance and the complexities that come with that.

 Object-oriented programming (OOP) is a programming paradigm that is based on the concept of objects (data). Instead of focusing on the functions and logics, OOP organizes software around data, or objects. A key concept in OOP is classes (sort of like templates). Suppose you want to display buttons in your application. Instead of writing the code to display each button individu ally, you can create a class to represent a generic button and use it to create buttons to display in your application. Each button has its own properties (characteristics). Using the concept of inheritance in OOP, you can create multiple subclasses of the button class to create different types of buttons, such as a rounded button, a rectangular button, and so on.

 Go has fewer features than other programming languages. You don’t have to worry about the best way to solve a problem — there is only one right way to solve a problem in Go. This makes your codebase easy to maintain.

 Go excels in concurrent programming. Go’s support for Goroutines makes it extremely easy to run multiple functions concurrently.

 Go has no support for generics (the ability to specify the actual data type until it’s actually used), but this may change as the language evolves.

 If you still aren’t convinced that you should learn Go, perhaps this next bit of news will motivate you: In the Stack Overflow Developer Survey 2019 (https:// insights.stackoverflow.com/survey/2019), Go developers were the third highest paid in the industry, behind Clojure and F# developers.

 Although Go has been around for quite a while (since 2009), only recently did it get wide adoption by developers, thanks to the proliferation of cloud computing and microservices. Today, Go has been widely used by major companies such as Dailymotion, Dropbox, Google, and Uber

 Here are some examples of where Go can be used

Cloud services: You can build scalable apps using Go on the Google Cloud Platform (GCP)

 Networking apps: With Go’s support for Goroutines, you can use Go to build distributed servers and application programming interfaces (APIs).

 Web services: You can use Go to build scalable and efficient web services.

 Command-line apps: Because Go runs on multiple platforms, you can compile the same codebase and target different platforms (such as those running on macOS and Windows)

 Installing Go on Your Machine

 You’re probably very eager to get started with Go programming on your machine, so let’s get to it!

The easiest way to install Go is to go to https://golang.org/doc/install. This website automatically detects the operating system (OS) you’re using and shows you the button to click to download the Go installer (see Figure 1-1)

macOS

 On macOS, the Go installer installs the Go distribution in the /usr/local/go directory. It also adds the /usr/local/go/bin directory to your PATH environment variable. You can verify this by entering the following command in the Terminal app (which you can find in the Applications/Utilities folder):

 You should see something like the following output (note the added path, high lighted in bold):

Windows

 On Windows, the Go installer installs the Go distribution in the C:\Go directory. It also adds the C:\Go\bin directory to your PATH environment variable. You can verify this by entering the following command in Command Prompt (which you can find by typing cmd in the Windows search box):

 You should see something like the following output (note the added path, high lighted in bold):

Using an Integrated Development Environment with Go

 To develop applications using Go, you just need a text editor (such as Visual Studio Code, TextEdit on macOS, or even the old trusty NotePad), and you’re good to go (pun unintended). However, many developers prefer to use integrated develop ment environments (IDEs) that can help them organize their code, as well as pro vide debugging support. Here is a partial list of IDEs that work with Go

 Visual Studio Code (https://code.visualstudio.com): Visual Studio Code from Microsoft is the mother of all code editors (and my personal favorite). Visual Studio Code is a full-featured code editor that supports almost all programming languages under the sun. Perhaps one of the most useful features of Visual Studio Code is IntelliSense, which helps to complete your statement as you type. It also comes with debugger support and an interac tive console, as well as Git integration. Best of all, Visual Studio Code is free and has a very active community of Go developers, allowing you to extend its functionalities through the various plug-ins

 GoLand (www.jetbrains.com/go/): GoLand is a cross-platform IDE by JetBrains. It comes with coding assistance, a debugger, an integrated Terminal, and more. GoLand is a commercial IDE, and it has a 30-day trial

The Go Playground (https://play.golang.org): The Go Playground (which isn’t really an IDE, but is worth a mention here) is a web service that runs on Go’s servers. It receives a Go program, compiles, links, runs it inside a sandbox, and then returns the output. The Go Playground is very useful when you need to test out some Go code quickly using a web browser.

Writing Your First Go Program

When you’re done typing, press ⌘ +S (Ctrl+S) to save the file. Name the file main. go. If this is the first time you’re writing a Go program using Visual Studio Code, it may prompt you to download additional plugins for Go. I recommend that you install the plugins.

 For this book, save your files to a folder named with the chapter number you’re reading. For example, save main.go in a folder named Chapter 1 in your home directory. On a Mac, that looks like this    

 After the file is saved, notice that your Go statements will now be color-coded — light blue for keywords such as package, import, and func; orange for strings like Hello, world! and fmt; and yellow for functions like main() and Println().


Working with Different Data Types

 This chapter explores one of the foundational building blocks of program ming: how to declare variables and constants in Go. This chapter also tells you how to manipulate strings in Go and how to convert data from one type to another.

 In Go, there are four types of data:

»Basic: Examples include strings, numbers, and Booleans. 

 »Aggregate: Examples include arrays and structs. 

 »Reference: Examples include pointers, slices, functions, and channels. 

 »Interface: An interface is a collection of method signatures

Declaring Always-Changing Variables

In programming, variables are containers that store values. These values may change over the lifetime of the program. In Go, you can declare and initialize vari ables in a variety of ways. I cover the possibilities in the following sections.

Using the var keyword: Type-inferred variables

The first way to declare a variable is to prefix the variable name with the var key word and then assign it a value, as in the following example:

 In Go, anything that follows the double-slash (//) is a comment. The compiler will ignore your comments, but adding comments to your code is usually a good practice because it makes it more understandable.

 In the preceding example, num1 is a variable whose type is an integer. Because it’s assigned the value of 5 during declaration, its type is inferred to be that of int (integer)

 In Go, a compilation error occurs if you declare a variable but you don’t make use of it (for example, by printing its value). So, in the previous example, one way to resolve this issue is to print it out using the Println() function. If you don’t resolve the issue, your program won’t compile.

Making Decisions

To make a program really useful, you need to be able to make decisions based on the values of variables and constants. Go offers a few constructs for making decisions

 » If/else statements » Switch statements » Select statements

 The third construct — the select statement — is for channel communication. I cover that subject in Chapter 12, where I explain more about channels.

 In this chapter, I explain how to make decisions in Go using the if/else and switch statements

Using If/Else Statements to Make Decisions

 The first method of making decisions in Go is the if/else statement. An if/else statement basically says, “Do x if such-and-such is true; otherwise, do y.” In the following sections, I walk you through how to use the if/else statement, starting with the foundation of decision making: logical and comparison operators

Laying the foundation for the if/else statement: Logical and comparison operators

 Before I get to the if/else statement, though, you need to know a little bit about how programming works. In Go, a Boolean (bool) variable can take on a value of true or false. This seemingly simple concept is the cornerstone of programming. Boolean values are used in programming to make comparisons and control the f low of programs. Without Boolean variables, there would be no programs! 

 To get a Boolean value, you usually use a comparison operator. Here’s a simple example

 In the preceding statements, I’m using the modulo (%) operator to check the remainder of a value divided by 2. If the remainder is 0, num is an even number. The following is a logical expression:

 The result is a Boolean value. It’s either true (if num contains an even number) or false (if num contains an odd number)

In the following examples, the first line (num := 6) is using the short variable declaration operator (:=), covered in Chapter 2, to basically say, “The variable num is assigned a value of 6.” In the following example, I’m using the == operator, which means “equal to.” Because 6 is not equal to 0, the result is false

 In the following example, I’m using the != operator, which means “not equal to.” Because 6 is not equal to 0, the result is true.

 In the following example, I’m using the < operator, which means “less than.” Because 6 is not less than 0, the result is false



No comments:

Post a Comment

Matter and Energy: Exploring the Stuff of Chemistry

 Introduction Congratulations on making a step toward discovering more about what I consider a fascinating subject: chemistry. For more than...