Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Go CookBook

You're reading from   Go CookBook Top techniques and practical solutions for real-life Go programming problems

Arrow left icon
Product type Paperback
Published in Dec 2024
Publisher Packt
ISBN-13 9781835464397
Length
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Burak Serdar Burak Serdar
Author Profile Icon Burak Serdar
Burak Serdar
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Preface 1. Chapter 1: Project Organization 2. Chapter 2: Working with Strings FREE CHAPTER 3. Chapter 3: Working with Date and Time 4. Chapter 4: Working with Arrays, Slices, and Maps 5. Chapter 5: Working with Types, Structs, and Interfaces 6. Chapter 6: Working with Generics 7. Chapter 7: Concurrency 8. Chapter 8: Errors and Panics 9. Chapter 9: The Context Package 10. Chapter 10: Working with Large Data 11. Chapter 11: Working with JSON 12. Chapter 12: Processes 13. Chapter 13: Network Programming 14. Chapter 14: Streaming Input/Output 15. Chapter 15: Databases 16. Chapter 16: Logging 17. Chapter 17: Testing, Benchmarking, and Profiling 18. Index 19. Other Books You May Enjoy

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.

lock icon The rest of the chapter is locked
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 $19.99/month. Cancel anytime
Banner background image