Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Mastering Go
  • Table Of Contents Toc
  • Feedback & Rating feedback
Mastering Go

Mastering Go

By : Mihalis Tsoukalos
4.8 (27)
close
close
Mastering Go

Mastering Go

4.8 (27)
By: Mihalis Tsoukalos

Overview of this book

Mastering Go, now in its fourth edition, remains the go-to resource for real-world Go development. This comprehensive guide delves into advanced Go concepts, including RESTful servers, and Go memory management. This edition brings new chapters on Go Generics and fuzzy Testing, and an enriched exploration of efficiency and performance. As you work your way through the chapters, you will gain confidence and a deep understanding of advanced Go topics, including concurrency and the operation of the Garbage Collector, using Go with Docker, writing powerful command-line utilities, working with JavaScript Object Notation (JSON) data, and interacting with databases. You will be engaged in real-world exercises, build network servers, and develop robust command-line utilities. With in-depth chapters on RESTful services, the WebSocket protocol, and Go internals, you are going to master Go's nuances, optimization, and observability. You will also elevate your skills in efficiency, performance, and advanced testing. With the help of Mastering Go, you will become an expert Go programmer by building Go systems and implementing advanced Go techniques in your projects.
Table of Contents (19 chapters)
close
close
16
Other Books You May Enjoy
17
Index

Hello World!

The following is the Go version of the Hello World! program. Please type it and save it as hw.go:

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

If you are eager to execute hw.go, type go run hw.go in the same directory where you save it. The file can also be found in the ch01 directory of the GitHub repository of the book.

Each Go source code begins with a package declaration. In this case, the name of the package is main, which has a special meaning in Go—autonomous Go programs should use the main package. The import keyword allows you to include functionality from existing packages. In our case, we only need some of the functionality of the fmt package that belongs to the standard Go library, implementing formatted input and output with functions that are analogous to C’s printf() and scanf(). The next important thing if you are creating an executable application is a main() function. Go considers this the entry point to the application, and it begins the execution of an application with the code found in the main() function of the main package.

hw.go is a Go program that runs on its own. Two characteristics make hw.go a source file that can generate an executable binary: the name of the package, which should be main, and the presence of the main() function—we discuss Go functions in more detail in the next subsection, but we will learn even more about functions and methods, which are functions attached to specific data types, in Chapter 6, Go Packages and Functions.

Introducing functions

Each Go function definition begins with the func keyword, followed by its name, signature, and implementation. Apart from the main() function, which has a special purpose, you can name the rest of your functions anything you want—there is a global Go rule that also applies to function and variable names and is valid for all packages except main: everything that begins with a lowercase letter is considered private and is accessible in the current package only. We will learn more about that rule in Chapter 6, Go Packages and Functions. The only exception to this rule is package names, which can begin with either lowercase or uppercase letters. Having said that, I am not aware of a Go package that begins with an uppercase letter!

You might now ask how functions are organized and delivered. Well, the answer is in packages—the next subsection sheds some light on that.

Introducing packages

Go programs are organized in packages—even the smallest Go program should be delivered as a package. The package keyword helps you define the name of a new package, which can be anything you want, with just one exception: if you are creating an executable application and not just a package that will be shared by other applications or packages, you should name your package main. You will learn more about developing Go packages in Chapter 6, Go Packages and Functions.

Packages can be used by other packages. In fact, reusing existing packages is a good practice that saves you from having to write lots of code or implement existing functionality from scratch.

The import keyword is used for importing other Go packages into your Go programs to use some or all of their functionality. A Go package can either be a part of the rich Standard Go library or come from an external source. Packages of the standard Go library are imported by name, for example, import "os" to use the os package, whereas external packages like github.com/spf13/cobra are imported using their full URLs: import "github.com/spf13/cobra".

Create a Note

Modal Close icon
You need to login to use this feature.
notes
bookmark search playlist download font-size

Change the font size

margin-width

Change margin width

day-mode

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Delete Bookmark

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Delete Note

Modal Close icon
Are you sure you want to delete it?
Cancel
Yes, Delete

Edit Note

Modal Close icon
Write a note (max 255 characters)
Cancel
Update Note

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY