Golang anatomy: Difference between revisions

From wikinotes
(Created page with "<syntaxhighlight lang="bash"> myproject/ go.mod // module name, requirements main.go // optional CLI entrypoint printer.go // other 'main' package src are in toplevel dir internal/ // exported symbols from internal packages are only exposed within 'myproject' logger/ // subpackage logger.go math/ // subpackage division.go multiplication.go </...")
 
No edit summary
Line 1: Line 1:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="go">
myproject/
myproject/
   go.mod                  // module name, requirements
   go.mod                  // module name, requirements

Revision as of 04:40, 20 June 2022

myproject/
  go.mod                   // module name, requirements
  main.go                  // optional CLI entrypoint
  printer.go               // other 'main' package src are in toplevel dir

  internal/                // exported symbols from internal packages are only exposed within 'myproject'
    logger/                // subpackage
      logger.go
    math/                  // subpackage
      division.go
      multiplication.go
// myproject/go.mod

module example.com/x/myproject

go 1.18
// myproject/main.go

package main

import (
    "fmt"
    "example.com/x/myproject/internal/logger"
)

func main() {
    logger.Info.Println("a log statement")
    fmt.Println("hello world")
}
// myproject/internal/logger/logger.go

package logger

var Info *log.Logger

func init() {
    Info = log.New(io.Stderr, "INFO: ", log.Ldate|log.Ltime|log.Llongfile)
}