Learn how to organize your projects and make sense of your GOPATH.
See how to use goroutines and channels to get more out of your servers' hardware.
Get the most out of the great tools that ship with Go.
Use profiling tools to squeeze every last big of performance out of your code.
Find out how to use Go's unique interfaces to write less code.
Make sure your code does what it advertises. Use Go's benchmark functionality to avoid performance regressions.
Get a taste of what's in store for you with these sample pages.
Computers have evolved, but programming languages haven't kept the same pace of evolution. The cell phone you carry probably has more CPU cores than the first computer you used. High powered servers now have 64, 128, or even more cores, but we're still programming for them in the same ways we were when there was just one core.
The art of programming has evolved too. Most programs aren't written by a single developer any more, they're written by teams of people sitting in different time zones, working at different times of the day. Large projects are broken up into smaller pieces and assigned to programmers who then deliver their work product back to the team in the form of a library or package that can be used across an entire suite of programs.
Today's programmers and companies believe more and more in the power of Open Source Software. Go is a programming language that makes sharing code easy. Go ships with tools that make it simple to use packages written by others, and Go makes it easy to share your own packages too.
In this chapter you'll see why Go is different from other programming languages. Go rethinks the traditional Object Oriented development you might be used to while still providing an efficient means for code reuse. Go makes it easier for you to effectively use all of the cores on your expensive server, and Go takes away the penalty of compiling a very large project.
As you read this chapter, you'll get a feeling for the many decisions that shaped the creation of Go, from its concurrency model to its lightning fast compiler. You'll appreciate the tools that ship with Go to make your life as a developer easier. You'll see why so many developers are choosing Go when they start up that new project.
Go has its own elegance and programming idioms which make the language very productive and fun to code in. In this chapter we will explore the syntax and programming structure of Go. If you are familiar with the C programming language or other derivatives, you will see a lot of similarities.
The Go language designers set out to create a programming language that would let them be productive without losing access to the lower level programming constructs they needed. This balance is achieved through a minimized set of keywords, built-in functions and minimized syntax. Go also provides a very comprehensive standard library. The standard library provides all the core packages programmers need to build real world, web and network based applications.
When we are done with this chapter, you will have a general overview of the Go language. We will familiarize ourselves with the general structure, style and syntax of the language. We will explore small coding examples that will provide an overview on variables, types, control structures, functions and error handling. All the things that make Go unique, productive and fun.
In chapter 2, you got an overview of the syntax and language structure of Go. Now we're going to dive deeper into how Go code is organized, and how you interact with Go code.
Packages are a critical concept in Go. By grouping similar code into packages, you enable code reuse and control the use of the data inside the package.
Before we get into the particulars, you should already be familiar with the command prompt or system shell, and you should have Go installed according to the guidelines in the preface of this book. If you're ready, let's start by understanding what a package is and why it is important in the Go ecosystem.
It is difficult to write programs that don't need to store and read collections of data. If we use databases, files or access the web, we need a way to handle the data we are receiving and sending. Go has three different data structures that allow us to manage collections of data: Arrays, Slices and Maps. These data structures are baked into the language and used throughout the standard library. Once you learn how these data structures work, programming in Go will become fun, fast and flexible.
Types are the heart and soul of a programming language. Types define how you represent and work with data, and they shape the interactions that you have with your code. For many, the type system is what really endears them to a programming language. There are two fundamental pieces to Go's type system: concrete types and interfaces types. Concrete types are used to store data into a section of memory. They can be simple like the built-in type int which stores an integer, or more complex like a struct type that stores information about a domain model like an order for a shopping website.
Interface types don't represent data, but instead declare functionality. An interface defines a function or set of functions that must be implemented in order for a type to satisfy the interface, and therefore be used in code that expects that particular interface. That's a pretty obtuse definition, to be certain, so we'll leave interfaces for the second half of this chapter where some concrete examples will make interfaces more clear.
Many programs can be written as one linear path of code that performs a single task and completes. When this is possible, always choose this option because these types of programs are usually much simpler to write and maintain. However, there are times when executing multiple tasks concurrently has greater benefit. One example is with a web service that can receive multiple requests for data on individual sockets at the same time. Each socket request is unique and can be independently processed from any other. Having the ability to execute requests concurrently can dramatically improve the performance of these types of systems. With this in mind, the language designers built concurrency directly into the language and runtime.
When a function is created as a goroutine, it is treated as an independent unit of work that gets scheduled and then executed on an available processor. The Go runtime has a sophisticated piece of software called the scheduler that manages all the goroutines that are created and need processor time. The scheduler is special because it sits on top of the operating system and controls everything related to the running of goroutines. No scheduling is done by the operating system. Having the runtime manage concurrency and not the operating system gives the runtime an advantage. It can maintain and control the state and memory requirements of each goroutine with greater efficiency which results in better overall performance.
In chapter 6 we learned what concurrency was, how channels behave and reviewed code that showed concurrency in action. In this chapter, we will extend that knowledge by reviewing more code. We will review three packages that implement different concurrency patterns that you can use in your own projects. Each package provides a practical perspective on the use of concurrency and channels and how they can make concurrent programs easier to write and reason about.
What is the Go standard library and why is it important? The Go standard library is a set of core packages that enhance and extend the language. These packages add to the number of different types of programs you can write without the need to build your own packages or download packages others have published. Since these packages are tied to the language, they come with some special guarantees:
These guarantees make the standard library special and something you want to leverage as much as possible. By using packages from the standard library, you make it easier on yourself to manage and have reliability in your own code. This is because you don't have to worry if your program is going to break between release cycles nor do you have to manage these dependencies.
Our book has an Author Online Forum where you can ask questions, provide feedback, and help shape the final content.
purchaseEnjoyed reading it. Looking forward to more.
To say it out loud: I am extremely impressed! I think this is one of THE best early-stage manuscripts I have seen. I think the authors do a tremendous job of presenting their material: clear, short, extremely to the point, no fluff (like Go itself!). No hype, either – what a relief!
People know about Go, but I don't think much has been written about it. And (based on what I have seen), this book would provide a tremendous “crash course” for intermediate to advanced programmers, who quickly want to get the gist of Go.
Covers a broad range of topics from language details to programming environment/ease-of-use features, ideal for someone with existing programming experience wanting to learn about Go.
"So far Go in Action is one of the best written language books i've ever read"
Brian, Erik, and Bill are part of the team behind Gopher Academy. We promote Go by helping to build a strong community of Go developers, through education, our blog, and the annual Go Conference.
Brian has been using Go in production since 2010. Working as the CIO of a credit bureau, Brian created the Skynet project to help move a monolithic Rails project into smaller, easier to maintain Go services. Brian's Go projects service several million API requests per day. @bketelsen
Erik created many of the projects that evolved out of Skynet, most notably SkyDNS. Erik has been a full-time Go developer since 2011. Previously, Erik worked on large web projects like Disney's online reservation system. @erikstmartin
William Kennedy is a managing partner at Ardan Studios in Miami, FL, a mobile, web app and systems development company. He is also a co-author of the book Go In Action , the author of the blog GoingGo.Net and the organizer for the Go and MongoDB meetup in Miami. Bill is focused on Go education and through his new venture Ardan Labs, Bill can often be found talking at conferences and giving workshops both locally and over hangouts. He always finds time to work with individuals and groups who want to take their Go knowledge, blogging and coding skills to the next level. @goinggodotnet