Search icon CANCEL
Subscription
0
Cart icon
Cart
Close icon
You have no products in your basket yet
Save more on your purchases!
Savings automatically calculated. No voucher code required
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Go - Fourth Edition

You're reading from  Mastering Go - Fourth Edition

Product type Book
Published in Mar 2024
Publisher Packt
ISBN-13 9781805127147
Pages 736 pages
Edition 4th Edition
Languages
Author (1):
Mihalis Tsoukalos Mihalis Tsoukalos
Profile icon Mihalis Tsoukalos
Toc

Table of Contents (19) Chapters close

Preface 1. A Quick Introduction to Go 2. Basic Go Data Types 3. Composite Data Types 4. Go Generics 5. Reflection and Interfaces 6. Go Packages and Functions 7. Telling a UNIX System What to Do 8. Go Concurrency 9. Building Web Services 10. Working with TCP/IP and WebSocket 11. Working with REST APIs 12. Code Testing and Profiling 13. Fuzz Testing and Observability 14. Efficiency and Performance 15. Changes in Recent Go Versions 16. Other Books You May Enjoy
17. Index
Appendix: The Go Garbage Collector

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".

You have been reading a chapter from
Mastering Go - Fourth Edition
Published in: Mar 2024 Publisher: Packt ISBN-13: 9781805127147
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime