Skip to content

Latest commit

 

History

History

02-package-and-module

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Package and Module

A package is a collection of source files in the same directory that are compiled together. A module is a collection of related Go packages that are released together.

Steps

1. Create a Go module

go mod init <module_name>
  1. If you want to use your module from other module. You can set github.com/<github user/org name>/<github repo name> to your module name.
  2. This command will generate a go.mod file.

Example:

go mod init github.com/nakamasato/kubernetes-operator-basics/05-golang-basics/02-package-and-module
go: creating new go.mod: module github.com/nakamasato/kubernetes-operator-basics/05-golang-basics/02-package-and-module

I specified the path to the current directory.

go.mod file will be created.

module github.com/nakamasato/kubernetes-operator-basics/05-golang-basics/02-module

go 1.19

2. Hello World

  1. Create a main.go.

    package main
    
    import "fmt"
    
    func main() {
        fmt.Println("Hello, World")
    }
  2. Run main.go

    go run main.go
    Hello, World
    

3. Use your own package

  1. Add mypackage. Create a mypackage/mypackage.go
    package mypackage
    
    func GetName() string {
        return "MyName"
    }
  2. Use mypackage in the module.
    1. Import the pacakge in main.go
      import (
          "fmt" // Packages in the standard library do not have a module path prefix
          "<module_name>/mypackage" // e.g. "github.com/nakamasato/kubernetes-operator-basics/05-golang-basics/02-module/mypackage"
      )
    2. Use GetName function in mypackage package.
      func main() {
          name := mypackage.GetName()
          fmt.Printf("Hello, %s\n", name)
      }
  3. Run main.go
    go run main.go
    Hello, MyName
    

4. Use an external package

  1. Import a package

    import "github.com/google/go-github/v47/github"
  2. Use it.

    client := github.NewClient(nil)
    // list all organizations for user "willnorris"
    orgs, _, err := client.Organizations.List(context.Background(), "willnorris", nil)
    if err != nil {
    	fmt.Println("Error")
    	os.Exit(1)
    }
    for i, org := range orgs {
    	fmt.Println(i, *org.Login)
    }
  3. Run go mod tidy go.mod is updated:

    require github.com/google/go-github/v47 v47.1.0
    
    require (
        github.com/google/go-querystring v1.1.0 // indirect
        golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
    )
    
  4. Run main.go

    go run main.go
    
    Hello, MyName
    0 diso
    1 activitystreams
    2 indieweb
    3 webfinger
    4 todogroup
    5 maintainers
    6 perkeep
    7 tailscale
    

Practice

  1. Packages, variables, and functions.
    1. Packages
    2. Imports
    3. Exported names
    4. Functions
    5. Functions continuedn
    6. Multiple results
    7. Named return values
    8. Variables
    9. Variables with initializers
    10. Short variable declarations
    11. Basic types
    12. Zero values
    13. Type conversions
    14. Type inference
    15. Constants
    16. Numeric Constants
  2. Flow control statements: for, if, else, switch and defer
    1. For
    2. For continued
    3. For is Go's "while"
    4. Forever
    5. If
    6. If with a short statement
    7. If and else
    8. Exercise: Loops and Functions
    9. Switch
    10. Switch evaluation order
    11. Switch with no condition
    12. Defer
    13. Stacking defers