O'Reilly logo

Go: Design Patterns for Real-World Projects by Mat Ryer, Mario Castro Contreras, Vladimir Vivien

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating programs

So far in the book, you have learned how to create and bundle Go code as reusable packages. A package, however, cannot be executed as a standalone program. To create a program (also known as a command), you take a package and define an entry point of execution as follows:

  • Declare (at least one) source file to be part of a special package called main
  • Declare one function name main() to be used as the entry point of the program

The function main takes no argument nor returns any value. The following shows the abbreviated source code for the main package used in the Ohm's Law example (from earlier). It uses the package flag, from Go's standard library, to parse program arguments formatted as flag:

package main import ( "flag" "fmt" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required