1. Directory structure
- lrn_package/src/hello/main.go
- lrn_package/somepackage/somefile.go
- lrn_package/internal/somefile.go
- lrn_package_use/src/hello/main.go
2. Programs
lrn_package/src/hello/main.go
package main
import (
"fmt"
"github.com/kamisuzuri/lrn_package/somepackage"
"github.com/kamisuzuri/lrn_package/internal"
)
func AddUpper() func (int) int {
var n int = 10
return func (x int) int {
n = n + x
return n
}
}
func main() {
ff := AddUpper()
fmt.Println(ff(1))
fmt.Println(ff(2))
fmt.Println(ff(3))
disp.SomeFunction()
itnldisp.SomeFunction()
}
lrn_package/somepackage/somefile.go
package disp
import "fmt"
func SomeFunction() {
fmt.Println("This is an public package function")
}
lrn_package/internal/somefile.go
package itnldisp
import "fmt"
func SomeFunction() {
fmt.Println("This is an internal package function")
}
lrn_package_use/src/hello/main.go
*comparing with lrn_package/src/hello/main.go, this one can only access somepackage which is published but not the internal package.
package main
import (
"fmt"
"github.com/kamisuzuri/lrn_package/somepackage"
// "github.com/kamisuzuri/lrn_package/internal"
)
func AddUpper() func (int) int {
var n int = 10
return func (x int) int {
n = n + x
return n
}
}
func main() {
ff := AddUpper()
fmt.Println(ff(1))
fmt.Println(ff(2))
fmt.Println(ff(3))
disp.SomeFunction()
// itnldisp.SomeFunction()
}
3. Publishing the package
- Go to directory: lrn_package
go mod init github.com/kamisuzuri/lrn_package
git init
git add .
git commit -m “Initial commitment”
git tag v1.0.3
git push origin main —tags
go build -o ./bin/lrngo ./src/hello/main.go (to build local for testing)
- Go to directory:
go mod init hello
go get github.com/kamisuzuri/lrn_packag
go build -o ./bin/lrngo ./src/hello/main.go
4. Updating modules in package published
-
1)Put a new tag on the package.
-
In directory lrn_package
git tag v1.0.4
git push origin main —tags
-
2)Get a new package
-
In the directory lrn_package_use
go get github.com/kamisuzuri/lrn_package@v1.0.4
go build -o ./bin/lrngo ./src/hello/main.go