Book Image

Hands-On Go Programming

By : Tarik Guney
Book Image

Hands-On Go Programming

By: Tarik Guney

Overview of this book

<p>With its C-like speed, simplicity, and power for a growing number of system-level programming domains, Go has become increasingly popular among programmers. Hands-On Go Programming teaches you the Go programming by solving commonly faced problems with the help of recipes. You will start by installing Go binaries and get familiar with the tools used for developing an application. Once you have understood these tasks, you will be able to manipulate strings and use them in built-in function constructs to create a complex value from two floating-point values. You will discover how to perform an arithmetic operation date and time, along with parsing them from string values. In addition to this, you will cover concurrency in Go, performing various web programming tasks, implementing system programming, reading and writing files, and honing many fundamental Go programming skills such as proper error handling and logging, among others. Whether you are an expert programmer or newbie, this book helps you understand how various answers are programmed in the Go language.</p>
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributor
Preface
Index

A quick look at the Go language


In this section, we're going to take a quick look at the Go programming language. Go is an expressive, concise, and clean language; it has concurrency mechanisms, and this helps programmers to write programs that get the most out of multi-core and networking machines. It also compiles quickly to machine code and has the convenience of garbage collection and the power of runtime reflection. It is a statically typed-in, compiled language, but, for most, it feels like a dynamically typed and interpreted language. All right then! Let's look at the syntax of Go by navigating to https://tour.golang.org/welcome/1; this is a good starting point for those who want to learn Go syntax:

OK, So if you look at the syntax in the screenshot, and if you come from languages such as Java and C#, or C and C++, you may find the syntax a little bit different. For instance, if you look at the return type, instead of defining the type, you actually define the return types at the end of your function. We also have a main function, which is the entry point of our application, similar to many other programming languages, and if you look at the context shown in the following screenshot, you can see that we have packages, variables, and functions, and flow control statements:  for, if...else, and types such as struct, slices, and maps:

 

If you want to create a class, such as structure, you can use a struct type and combine it with a pointer. Additionally, it has methods and interfaces and concurrency, but it doesn't have generics.

Having said that, I will also talk about the tools that I'm going to be using throughout this book. There are a couple of tools available in GoLand. GoLand is a relatively new IDE by JetBrains. We will be using GoLand throughout the book. You can easily create new projects and give them a name and choose the SDK, which is Go 1.9. You can also add new files or new packages, and so on.

You can define your configurations, and build your Go, by typing your entry file, as shown in the following screenshot. You can then run  main.goand click OK:

 

Finally, pressing Ctrl r will build your project, as can be seen in the following screenshot:

Before I conclude this chapter, let me quickly show you an example that uses just the terminal. I'm going to use the  touch command to create the main.go file and add the following code:

package main
import "fmt"
func main(){
 fmt.Println(a:"Hello World")
}

 

 

You can run it by using the go run main.go command and you will get the following output:

You can save it and then run it. So, this is how you can use the terminal to quickly write Go code and run it.