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 Go Recipes for Developers
  • Table Of Contents Toc
Go Recipes for Developers

Go Recipes for Developers

By : Burak Serdar
close
close
Go Recipes for Developers

Go Recipes for Developers

By: Burak Serdar

Overview of this book

With its simple syntax and sensible conventions, Go has emerged as the language of choice for developers in network programming, web services, data processing, and other settings. This practical guide helps engineers leverage Go through up-to-date recipes that solve common problems in day-to-day programming. Drawing from three decades of distributed systems engineering and technical leadership at companies like Red Hat, Burak Serdar brings battle-tested expertise in building robust, scalable applications. He starts by covering basics of code structure, describing different approaches to organizing packages for different types of projects. You’ll discover practical solutions to engineering challenges in network programming, dealing with processes, databases, data processing pipelines, and testing. Each chapter provides working solutions and production-ready code snippets that you can seamlessly incorporate into your programs while working in sequential and concurrent settings. The solutions leverage the more recent additions to the Go language, such as generics and structured logging. Most of the examples are developed using the Go standard library without any third-party packages. By the end of this book, you’ll have worked through a collection of proven recipes that will equip you accelerate your Go development journey.
Table of Contents (20 chapters)
close
close

Building and running programs

Now that you have a module and a source tree with some Go files, you can build or run your program.

How to do it...

  • Use go build to build the current package
  • Use go build ./path/to/package to build the package in the given directory
  • Use go build <moduleName> to build a module
  • Use go run to run the current main package
  • Use go run ./path/to/main/package to build and run the main package in the given directory
  • Use go run <moduleName/mainpkg> to build and run the module’s main under the given directory

Let’s write the main function that starts an HTTP server. The following snippet is cmd/webform/main.go:

package main
import (
    "net/http"
)
func main() {
    server := http.Server{
        Addr:    ":8181",
        Handler: http.FileServer(http.Dir("web/static")),
    }
    server.ListenAndServe()
}

Currently, main only imports the standard library’s net/http package. It starts a server that serves the files under the web/static directory. Note that for this to work, you have to run the program from the module root:

$ go run ./cmd/webform

Always run the main package; avoid go run main.go. This will run main.go, excluding any other files in the main package. It will fail if you have other .go files that contain helper functions in the main package.

If you run this program from another directory, it will fail to find the web/static directory; because it is a relative path, it is resolved relative to the current directory.

When you run a program via go run, the program executable is placed in a temporary directory. To build the executable, use the following:

$ go build ./cmd/webform

This will create a binary in the current directory. The name of the binary will be determined by the last segment of the main package – in this case, webform. To build a binary with a different name, use the following:

$ go build -o wform ./cmd/webform

This will build a binary called wform.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Go Recipes for Developers
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist download Download options font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

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

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon