Golang anatomy: Difference between revisions

From wikinotes
No edit summary
No edit summary
Line 27: Line 27:


{{expand
{{expand
| myproject.main.go
| myproject/main.go
|
|
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">

Revision as of 04:42, 20 June 2022

Project Structure

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
// myproject/go.mod

module example.com/x/myproject

go 1.18
myproject/main.go
// 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
// 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)
}