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 </...")
 
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
This page is a general getting started in go.
= Project Structure =
<blockquote>
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
myproject/
myproject/
   go.mod                  // module name, requirements
   go.mod                  # module name, requirements
   main.go                  // optional CLI entrypoint
   main.go                  # 'main' package's 'main()' function is optional CLI entrypoint
   printer.go              // other 'main' package src are in toplevel dir
   printer.go              # other 'main' package src are in toplevel dir


   internal/                // exported symbols from internal packages are only exposed within 'myproject'
   internal/                # exported symbols from internal packages are only exposed within 'myproject'
     logger/                // subpackage
     logger/                # subpackage
       logger.go
       logger.go
     math/                  // subpackage
     math/                  # subpackage
       division.go
       division.go
       multiplication.go
       multiplication.go
</syntaxhighlight>
</syntaxhighlight>


{{expand
| myproject/go.mod
|
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
// myproject/go.mod
// myproject/go.mod
Line 20: Line 27:
go 1.18
go 1.18
</syntaxhighlight>
</syntaxhighlight>
}}


{{expand
| myproject/main.go
|
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
// myproject/main.go
// myproject/main.go
Line 28: Line 39:
import (
import (
     "fmt"
     "fmt"
    "os"
     "example.com/x/myproject/internal/logger"
     "example.com/x/myproject/internal/logger"
)
)
ErrEnvVarUnset = errors.New("Environment Variable Not Set")
func getUserName() (name string, err error) {
    username := os.Getenv("USER")
    if username == "" {
        return nil, ErrEnvVarUnset;
    }
    return username, nil;
}


func main() {
func main() {
     logger.Info.Println("a log statement")
     logger.Info.Println("a log statement");
     fmt.Println("hello world")
 
    name, err := getUserName();  // type inference
    if err != nil {
        panic(err);
    }
 
    var gid int16;                // declare var
    gid = 1000;                  // assign typed var
 
     fmt.Printf("logged in as user: %s, with gid %i", name, gid);
}
}
</syntaxhighlight>
</syntaxhighlight>
}}


{{expand
| myproject/internal/logger/logger.go
|
<syntaxhighlight lang="go">
<syntaxhighlight lang="go">
// myproject/internal/logger/logger.go
// myproject/internal/logger/logger.go
Line 48: Line 83:
}
}
</syntaxhighlight>
</syntaxhighlight>
}}
</blockquote><!-- Project Structure -->
= Tools =
<blockquote>
<syntaxhighlight lang="bash">
# project management
go mod init foo.net/x/my-project  # create new project
$GOPRIVATE                        # glob-matches projects not published to public registry
# requirements
go get example.com/module@latest  # add/update requirement (latest)
go get example.com/module v1.1.1  # add/update/downgrade to target version
# package management
go install example.com/module@latest  # install tool
# documentation
go doc io      # functions/constants on 'io'
go doc io.File  # methods on 'io.File'
# build/run main package
go run .
# test
# build management
go build
go clean
go install
</syntaxhighlight>
</blockquote><!-- Tools -->

Latest revision as of 04:16, 29 January 2023

This page is a general getting started in go.

Project Structure

myproject/
  go.mod                   # module name, requirements
  main.go                  # 'main' package's 'main()' function is 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"
    "os"
    "example.com/x/myproject/internal/logger"
)

ErrEnvVarUnset = errors.New("Environment Variable Not Set")

func getUserName() (name string, err error) {
    username := os.Getenv("USER")
    if username == "" {
        return nil, ErrEnvVarUnset;
    }
    return username, nil;
}

func main() {
    logger.Info.Println("a log statement");

    name, err := getUserName();   // type inference
    if err != nil {
        panic(err);
    }

    var gid int16;                // declare var
    gid = 1000;                   // assign typed var 

    fmt.Printf("logged in as user: %s, with gid %i", name, gid);
}

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)
}

Tools

# project management
go mod init foo.net/x/my-project  # create new project
$GOPRIVATE                        # glob-matches projects not published to public registry

# requirements
go get example.com/module@latest  # add/update requirement (latest)
go get example.com/module v1.1.1  # add/update/downgrade to target version

# package management
go install example.com/module@latest  # install tool

# documentation
go doc io       # functions/constants on 'io'
go doc io.File  # methods on 'io.File'

# build/run main package
go run .

# test

# build management
go build
go clean
go install