Showing posts with label golang_module. Show all posts
Showing posts with label golang_module. Show all posts

Mar 8, 2021

[Go] relase v1.16 highlight

Reference:
https://golang.org/doc/go1.16




386:
As announced in the Go 1.15 release notes, Go 1.16 drops support for x87 mode compilation (GO386=387). Support for non-SSE2 processors is now available using soft float mode (GO386=softfloat). Users running on non-SSE2 processors should replace GO386=387 with GO386=softfloat.


Module:
Module-aware mode is enabled by default, regardless of whether a go.mod file is present in the current working directory or a parent directory.


More precisely, the GO111MODULE environment variable now defaults to on. To switch to the previous behavior, set GO111MODULE to auto.

Build commands like go build and go test no longer modify go.mod and go.sum by default.
Instead, they report an error if a module requirement or checksum needs to be added or updated (as if the -mod=readonly flag were used). Module requirements and sums may be adjusted with go mod tidy or go get.

go install now accepts arguments with version suffixes (for example, go install example.com/cmd@v1.0.0 or go install example.com/cmd@latest). This causes go install to build and install packages in module-aware mode, ignoring the go.mod file in the current directory or any parent directory, if there is one. This is useful for installing executables without affecting the dependencies of the main module.

go install, with or without a version suffix (as described above), is now the recommended way to build and install packages in module mode.
go get should be used with the -d flag to adjust the current module's dependencies without building packages, and use of go get to build and install packages is deprecated. In a future release, the -d flag will always be enabled.

retract directives may now be used in a go.mod file to indicate that certain published versions of the module should not be used by other modules. A module author may retract a version after a severe problem is discovered or if the version was published unintentionally.

The "retract" directive comes in two forms:

retract v1.0.0 // single version
retract [v1.1.0, v1.2.0] // closed interval

The go directive for specifying a Go version does not seem to matter when using this. E.g. I can have a file like this:
module awesomeProject37

go 1.11 // not Go 1.16 above

retract v1.0.0 // single version
retract [v1.1.0, v1.2.0] // closed interval
As long as using Go 1.16+ as the SDK.


The go mod vendor and go mod tidy subcommands now accept the -e flag, which instructs them to proceed despite errors in resolving missing packages.

The go command now ignores requirements on module versions excluded by exclude directives in the main module. Previously, the go command used the next version higher than an excluded version, but that version could change over time, resulting in non-reproducible builds.

In module mode, the go command now disallows import paths that include non-ASCII characters or path elements with a leading dot character (.)
Module paths with these characters were already disallowed (see Module paths and versions), so this change affects only paths within module subdirectories.


Embedding Files:
The go command now supports including static files and file trees as part of the final executable, using the new //go:embed directive. See the documentation for the new embed package for details.


go test:
When using go test, a test that calls os.Exit(0) during execution of a test function will now be considered to fail. This will help catch cases in which a test calls code that calls os.Exit(0) and thereby stops running all future tests. If a TestMain function calls os.Exit(0) that is still considered to be a passing test.

go test reports an error when the -c or -i flags are used together with unknown flags. Normally, unknown flags are passed to tests, but when -c or -i are used, tests are not run.


go get:
The go get -insecure flag is deprecated and will be removed in a future version. This flag permits fetching from repositories and resolving custom domains using insecure schemes such as HTTP, and also bypasses module sum validation using the checksum database.

To permit the use of insecure schemes, use the GOINSECURE environment variable instead. 
To bypass module sum validation, use GOPRIVATE or GONOSUMDB. See go help environment for details.


go get example.com/mod@patch now requires that some version of example.com/mod already be required by the main module. (However, go get -u=patch continues to patch even newly-added dependencies.)


GOVCS environment variable:
GOVCS is a new environment variable that limits which version control tools the go command may use to download source code. This mitigates security issues with tools that are typically used in trusted, authenticated environments. By default, git and hg may be used to download code from any repository. svn, bzr, and fossil may only be used to download code from repositories with module paths or package paths matching patterns in the GOPRIVATE environment variable. See go help vcs for details.


The all pattern:
When the main module's go.mod file declares go 1.16 or higher, the all package pattern now matches only those packages that are transitively imported by a package or test found in the main module. (Packages imported by tests of packages imported by the main module are no longer included.) This is the same set of packages retained by go mod vendor since Go 1.11.


The -toolexec build flag:
When the -toolexec build flag is specified to use a program when invoking toolchain programs like compile or asm, the environment variable TOOLEXEC_IMPORTPATH is now set to the import path of the package being built.


The -i build flag:
The -i flag accepted by go build, go install, and go test is now deprecated. 


The list command:
When the -export flag is specified, the BuildID field is now set to the build ID of the compiled package.
This is equivalent to running go tool buildid on go list -exported -f {{.Export}}, but without the extra step.


The -overlay flag:
The -overlay flag specifies a JSON configuration file containing a set of file path replacements. The -overlay flag may be used with all build commands and go mod subcommands. It is primarily intended to be used by editor tooling such as gopls to understand the effects of unsaved changes to source files. The config file maps actual file paths to replacement file paths and the go command and its builds will run as if the actual file paths exist with the contents given by the replacement file paths, or don't exist if the replacement file paths are empty.


Cgo:
The cgo tool will no longer try to translate C struct bitfields into Go struct fields, even if their size can be represented in Go. The order in which C bitfields appear in memory is implementation dependent, so in some cases the cgo tool produced results that were silently incorrect.


Runtime:
The new runtime/metrics package introduces a stable interface for reading implementation-defined metrics from the Go runtime. It supersedes existing functions like runtime.ReadMemStats and debug.GCStats and is significantly more general and efficient. See the package documentation for more details.

Setting the GODEBUG environment variable to inittrace=1 now causes the runtime to emit a single line to standard error for each package init, summarizing its execution time and memory allocation.

This trace can be used to find bottlenecks or regressions in Go startup performance. The GODEBUG documentation describes the format.

On Linux, the runtime now defaults to releasing memory to the operating system promptly (using MADV_DONTNEED), rather than lazily when the operating system is under memory pressure (using MADV_FREE).
This means process-level memory statistics like RSS will more accurately reflect the amount of physical memory being used by Go processes. Systems that are currently using GODEBUG=madvdontneed=1 to improve memory monitoring behavior no longer need to set this environment variable.

Go 1.16 fixes a discrepancy between the race detector and the Go memory model. The race detector now more precisely follows the channel synchronization rules of the memory model. As a result, the detector may now report races it previously missed.


Compiler:
The compiler can now inline functions with 
  • non-labeled for loops, 
  • method values, 
  • and type switches.
The inliner can also detect more indirect calls where inlining is possible.



Core library:
The new io/fs package defines the fs.FS interface.
Deprecation of io/ioutil.

Feb 27, 2020

[Go] 1.14 notes

Release note: https://golang.org/doc/go1.14

Overlapping interface:

Proposal: https://github.com/golang/proposal/blob/master/design/6977-overlapping-interfaces.md



Language spec:

https://tip.golang.org/ref/spec#Uniqueness_of_identifiers
Given a set of identifiers, an identifier is called unique if it is different from every other in the set.
Two identifiers are different if they are spelled differently, or if they appear in different packages and are not exported. Otherwise, they are the same.
e.g
https://play.golang.org/p/S6KnfCj7UbV



WebAssembly:

JavaScript values referenced from Go via js.Value objects can now be garbage collected.
js.Value values can no longer be compared using the == operator, and instead must be compared using their Equal method.
js.Value now has IsUndefined, IsNull, and IsNaN methods.



Module:

Vendoring:

When the main module contains a top-level vendor directory and its go.mod file specifies go 1.14 or higher, the go command now defaults to
-mod=vendor
for operations that accept that flag.
A new value for that flag,
-mod=mod
causes the go command to instead load modules from the module cache
(as when no vendor directory is present).

When
-mod=vendor is set (explicitly or by default),
the go command now verifies that the main module's vendor/modules.txt file is consistent with its go.mod file.

$ go list -m
no longer silently omits transitive dependencies that do not provide packages in the vendor directory.
It now fails explicitly if -mod=vendor is set and information is requested for a module not mentioned in vendor/modules.txt



Flags:

-mod=readonly is now set by default when the go.mod file is read-only and no top-level vendor directory is present.

-modcacherw is a new flag that instructs the go command to leave newly-created directories in the module cache at their default permissions rather than making them read-only.
The use of this flag makes it more likely that tests or other tools will accidentally add files not included in the module's verified checksum. However, it allows the use of rm -rf (instead of go clean -modcache) to remove the module cache.

-modfile=file is a new flag that instructs the go command to read (and possibly write) an alternate go.mod file instead of the one in the module root directory.
A file named go.mod must still be present in order to determine the module root directory, but it is not accessed.
When -modfile is specified, an alternate go.sum file is also used: its path is derived from the -modfile flag by trimming the .mod extension and appending .sum.



Environment variables:

GOINSECURE is a new environment variable that instructs the go command to not require an HTTPS connection, and to skip certificate validation, when fetching certain modules directly from their origins.
Like the existing GOPRIVATE variable, the value of GOINSECURE is a comma-separated list of glob patterns.



Commands outside modules:

When module-aware mode is enabled explicitly (by setting GO111MODULE=on), most module commands have more limited functionality if no go.mod file is present.
For example, go build, go run, and other build commands can only build packages in the standard library and packages specified as .go files on the command line.

Previously, the go command would resolve each package path to the latest version of a module but would not record the module path or version. This resulted in slow, non-reproducible builds.

go get continues to work as before, as do go mod download and go list -m with explicit versions.



go.mod file maintenance:

go commands other than go mod tidy no longer remove a require directive that specifies a version of an indirect dependency that is already implied by other (transitive) dependencies of the main module.

go commands other than go mod tidy no longer edit the go.mod file if the changes are only cosmetic.

When -mod=readonly is set, go commands will no longer fail due to a missing go directive or an erroneous // indirect comment.



Testing:

go test -v now streams t.Log output as it happens, rather than at the end of all tests.



Runtime:

defer can now be used in performance-critical code without overhead concerns.

Goroutines are now asynchronously preemptible.
To turn off avoid kernel bugs:
$ GODEBUG=asyncpreemptoff=1


EINTR on close() means the fd has already closed even there's an interruption.
(This can occur because the Linux kernel always releases
the file descriptor early in the close operation, freeing it for
reuse; the steps that may return an error, such as flushing data to
the filesystem or device, occur only later in the close operation.)

Prevent blocking and handles EINTR:
// http://250bpm.com/blog:12

// set file descriptor to non-blocking;
sigprocmask() to block SIGINT;

if (stop) { // handle it }

while (1) {
    pselect() with sigmask argument which doesn't block SIGINT;
    if (stop) { // handle it }
    recv();
}


Reference:
http://250bpm.com/blog:12
https://groups.google.com/forum/#!msg/golang-nuts/oqXOnR1GJ4g/Rrok3fUqAQAJ

Go issue ticket:
runtime: memory corruption on Linux 5.2+ :
https://github.com/golang/go/issues/35777

runtime: mlock of signal stack failed:
https://github.com/golang/go/issues/37436
(monitor /proc/version for linux kernel correct version)

runtime, syscall: occasional syscall failures with EINTR within ZMQ when using Go1.14beta1 but not Go1.11:
https://github.com/golang/go/issues/36281
https://github.com/pebbe/zmq4/issues/17

Kernel ticket:
https://bugzilla.kernel.org/show_bug.cgi?id=205663


A consequence of the implementation of preemption is that on Unix systems, including Linux and macOS systems, programs built with Go 1.14 will receive more signals than programs built with earlier releases.

This means that programs that use packages like syscall or golang.org/x/sys/unix will see more slow(i.e block system calls) system calls fail with EINTR errors.
(Recap: TLPI: 21.5 Interruption and Restarting of System Calls)

Those programs will have to handle those errors in some way, most likely looping to try the system call again.
For more information about this see man 7 signal(http://man7.org/linux/man-pages/man7/signal.7.html) for Linux systems or similar documentation for other systems.

The page allocator is more efficient and incurs significantly less lock contention at high values of GOMAXPROCS




Compiler:

This release adds -d=checkptr as a compile-time option for adding instrumentation to check that Go code is following unsafe.Pointer safety rules dynamically.

This option is enabled by default (except on Windows) with the -race or -msan flags, and can be disabled with -gcflags=all=-d=checkptr=0.

-d=checkptr checks the following:

  1. When converting unsafe.Pointer to *T, the resulting pointer must be aligned appropriately for T.
  2. If the result of pointer arithmetic points into a Go heap object, one of the unsafe.Pointer-typed operands must point into the same object.


The compiler can now emit machine-readable logs of key optimizations using the -json flag, including
  • inlining
  • escape analysis
  • bounds-check elimination
  • nil-check elimination.


Detailed escape analysis diagnostics (-m=2) now work again.

This release includes experimental support for compiler-inserted coverage instrumentation for fuzzing.

Bounds check elimination now uses information from slice creation and can eliminate checks for indexes with types smaller than int.



Library:

reflect
StructOf now supports creating struct types with unexported fields, by setting the PkgPath field in a StructField element.


https://buttondown.email/cryptography-dispatches/archive/cryptography-dispatches-new-crypto-in-go-114/

Dec 7, 2019

[Go] module version design reasoning

Reference:
https://research.swtch.com/vgo-principles


Go modules uses import path syntax called semantic import versioning, along with a new algorithm for selecting which versions to use, called minimal version selection.


For Go modules, the import compatibility rule can be written as:
If an old package and a new package have the same import path,
the new package must be backwards compatible with the old package.



Semantic import versioning:




For major version difference build:



e.g



For minor version difference build (consider repeatability):

The algorithm used for Go modules is very simple, despite the imposing name "minimal version selection"

It works like this:
  1. Each package specifies a minimum version of each dependency. For example, suppose B 1.3 requests D 1.3 or later, and C 1.8 requests D 1.4 or later.
  2. In Go modules, the go command prefers to use those exact versions, not the latest versions. If we’re building B by itself, we’ll use D 1.3. If we’re building C by itself, we’ll use D 1.4. The builds of these libraries are repeatable.

Also shown in the figure, if different parts of a build request different minimum versions, the go command uses the latest requested version.

The build of A sees requests for D 1.3 and D 1.4, and 1.4 is later than 1.3, so the build chooses D 1.4. That decision does not depend on whether D 1.5 and D 1.6 exist, so it does not change over time.

Sep 2, 2019

[Go][tricks] API compatibility

Reference:
GopherCon 2019: Jonathan Amsterdam - Detecting Incompatible API Changes
https://www.youtube.com/watch?v=JhdL5AkH-AQ
Slides:
https://about.sourcegraph.com/go/gophercon-2019-detecting-incompatible-api-changes
Spec:
https://go.googlesource.com/exp/+/refs/heads/master/apidiff/README.md
Tools binary:
$ go get golang.org/x/exp/cmd/apidiff  # will be replaced by gorelease
Golang /x/tools/go/packages
https://godoc.org/golang.org/x/tools/go/packages
Golang types package
https://golang.org/pkg/go/types/
Golang constant package
https://golang.org/pkg/go/constant/
Golang gcexportdata package
https://godoc.org/golang.org/x/tools/go/gcexportdata


A comparison of two interface values with identical dynamic types causes a run-time panic if values of that type are not comparable.
This behavior applies not only to direct interface value comparisons but also when comparing arrays of interface values or structs with interface-valued fields.

Go v1.15
Use an unexported, zero-width, non-comparable field
(Function values, Slice values and Map values are not comparable
https://golang.org/ref/spec#Comparison_operators ), to
prevent clients from comparing a struct and shrink binary size.
(Using 0 size of array instead of pure Function/Slice/Map which holds a size
of pointer points to memory):
type Point struct {
 _ [0] func()
 X, Y int
}

Sep 11, 2018

[Go] [go module] wrap up

Reference:


Definition:

Modules:

  • A module is a collection of related Go packages that are versioned together as a single unit. 
  • Modules must be semantically versioned in the form v(major).(minor).(patch), such as v0.1.0, v1.2.3, or v3.0.1.
    pre-release version
  • The versioning leading v is required.
  • The go command automatically updates go.mod each time it uses the module graph, to make sure go.mod always accurately reflects reality and is properly formatted.
  • Because the module graph defines the meaning of import statements, any commands that load packages also use and therefore update go.mod, including go build, go get, go install, go list, go test, go mod graph, go mod tidy, and go mod why.
    $ go mod why -m rsc.io/binaryregexp



Commands outside modules:

When module-aware mode is enabled explicitly (by setting GO111MODULE=on), most module commands have more limited functionality if no go.mod file is present.

For example, go build, go run, and other build commands can only build packages in the standard library and packages specified as .go files on the command line.

Previously, the go command would resolve each package path to the latest version of a module but would not record the module path or version. This resulted in slow, non-reproducible builds.

go get continues to work as before, as do go mod download and go list -m with explicit versions.
go list -m // list module instead of packages


Default Proxy:
https://proxy.golang.org

Default checksum db:
https://sum.golang.org

ENV: (reference: https://golang.org/doc/go1.13#modules)

  • $GOPRIVATE (show: $ go env GOPRIVATE)
    controls which modules the go command considers to be private (not available publicly) and should therefore not use the proxy or checksum database. The variable is a comma-separated list of glob patterns (in the syntax of Go's path.Match) of module path prefixes.
  • $GOMOD (show: $ go env GOMOD)
    The absolute path to the go.mod of the main module,
    or the empty string if not using modules.
  • $GOPROXY (show: $ go env GOPROXY)
    URL of Go module proxy. See 'go help goproxy'.
  • $GOTOOLDIR (show: $ go env GOTOOLDIR)
    The directory where the go tools (compile, cover, doc, etc...) are installed.
  • $GOHOSTOS (show: $ go env GOHOSTOS)
    The operating system (GOOS) of the Go toolchain binaries.
  • $GOHOSTARCH (show: $ go env GOHOSTARCH)
    The architecture (GOARCH) of the Go toolchain binaries.
  • $GOEXE (show: $ go env GOEXE)
    The executable file name suffix (".exe" on Windows, "" on other systems).
  • $GOTMPDIR (show: $ go env GOTMPDIR)
    The directory where the go command will write temporary source files, packages, and binaries.
  • $GOINSECURE is a new environment variable that instructs the go command to not require an HTTPS connection, and to skip certificate validation, when fetching certain modules directly from their origins.
    Like the existing GOPRIVATE variable, the value of GOINSECURE is a comma-separated list of glob patterns.



go.mod example:

--
module my/thing
go 1.12
require other/thing v1.0.2
require new/thing/v2 v2.3.4
exclude old/thing v1.2.3
replace bad/thing v1.4.5 => good/thing v1.4.5
--

verbs explain:
--
module, to define the module path;
go, to set the expected language version;
require, to require a particular module at a given version or later;
exclude, to exclude a particular module version from use; and
replace, to replace a module version with a different module version.
--

like go import's syntax, verbs can be coded as:
--
require (
    new/thing v2.3.4
    old/thing v1.2.3
)
--


GOPATH and Modules:

When using modules, GOPATH is no longer used for resolving imports.
However, it is still used to store downloaded source code (in GOPATH/pkg/mod)
and compiled commands (in GOPATH/bin).

'go get' checks out or updates a git repository also updates submodules.



Module proxy protocol:

  1. If GOPROXY is unset, is the empty string, or is the string "direct", downloads use the default direct connection to version control systems.
  2. Setting GOPROXY to "off" disallows downloading modules from any source.
  3. Otherwise, GOPROXY is expected to be the URL of a module proxy, in which case the go command will fetch all modules from that proxy.
  4. No matter the source of the modules, downloaded modules must match existing entries in go.sum (see 'go help modules' for discussion of verification).



golang build cache path:

The go command also caches successful package test results. 
See 'go help test' for details.

GOCACHE, defaulting to $HOME/.cache/go-build, we'll set to "/tmp/on"

Print current cache directory
$ go env GOCACHE

Setting the environment variable GOCACHE=off will cause go commands that write to the cache to fail.
GOCACHE=off 

Clean all cache
go clean -cache

Removes all cached test results
$ go clean -testcache


The GODEBUG environment variable can enable printing of debugging information about the state of the cache:
  • GODEBUG=gocacheverify=1 causes the go command to bypass the use of any cache entries and instead rebuild everything and check that the results match existing cache entries. 
  • GODEBUG=gocachehash=1 causes the go command to print the inputs for all of the content hashes it uses to construct cache lookup keys. The output is voluminous but can be useful for debugging the cache. 
  • GODEBUG=gocachetest=1 causes the go command to print details of its decisions about whether to reuse a cached test result.


golang module cache path:

$ GOPATH/pkg/mod
We can remove the cached package manually here.




Clean downloaded cache:

$ go clean -modcache
reference: https://tip.golang.org/cmd/go/#hdr-Remove_object_files_and_cached_files





Backward compatible:

Set GO111MODULE=on if want to use go module within $GOPATH



Modules and vendoring

When the main module contains a top-level vendor directory and its go.mod file specifies go 1.14 or higher, the go command now defaults to -mod=vendor

When -mod=vendor is set (explicitly or by default), the go command now verifies that the main module's vendor/modules.txt file is consistent with its go.mod file.

By default, the go command satisfies dependencies by downloading modules from their sources and using those downloaded copies (after verification).

-mod=mod causes the go command to instead load modules from the module cache.

To allow interoperation with older versions of Go, or to ensure that all files used for a build are stored together in a single file tree, 'go mod vendor' creates a directory named vendor in the root directory of the main module and stores there all the packages from dependency modules that are needed to support builds and tests of packages in the main module.

To build using the main module's top-level vendor directory to satisfy dependencies (disabling use of the usual network sources and local caches), use 'go build -mod=vendor'.   (This is not recommended)
i.e put all dependencies under:
vendor/ folder



Note that only the main module's top-level vendor directory is used; vendor directories in other locations are still ignored.



Readonly Modules

If invoked with -mod=readonly, the go command is disallowed from the implicit automatic updating of go.mod described above.

When -mod=readonly is set, go commands will no longer fail due to a missing go directive or an erroneous // indirect comment.

-mod=readonly is now set by default when the go.mod file is read-only and no top-level vendor directory is present.

Instead, it fails when any changes to go.mod are needed. This setting is most useful to check that go.mod does not need updates, such as in a continuous integration and testing system.

The "go get" command remains permitted to update go.mod even with -mod=readonly, and the "go mod" commands do not take the -mod flag (or any other build flags).


Cache Control

-modcacherw is a new flag that instructs the go command to leave newly-created directories in the module cache at their default permissions rather than making them read-only.

The use of this flag makes it more likely that tests or other tools will accidentally add files not included in the module's verified checksum. However, it allows the use of rm -rf (instead of go clean -modcache) to remove the module cache.



Use other go.mod file

-modfile=file is a new flag that instructs the go command to read (and possibly write) an alternate go.mod file instead of the one in the module root directory.

A file named go.mod must still be present in order to determine the module root directory, but it is not accessed.

When -modfile is specified, an alternate go.sum file is also used: its path is derived from the -modfile flag by trimming the .mod extension and appending .sum.



Fresh up your git tag skills:

go module relies on git tag to do versioning.

Two kinds of git tags:

https://git-scm.com/book/en/v2/Git-Basics-Tagging
Annotated tag, as a commit object with tagger's information:
$ git tag -a v1.0.1 -m "version 1.0.1"
$ git show v1.0.1

Lightweight Tags, no information attached:
$ git tag v1.0.1-lw

Push local tags to remote:
$ git push origin <tagname>
push all tags:
$ git push origin --tags

Checking out Tags:
Will be in detached state. i.e not under current branch's HEAD commit.
$ git checkout 2.0.0

or checkout tag into branch: (better, thus you could do some changes and commit)
$ git checkout -b version2 v2.0.0


delete local tag:
https://stackoverflow.com/questions/5480258/how-to-delete-a-git-remote-tag
$ git tag -d v1.4


delete remote tag:
$ git push origin :refs/tags/v1.4
or
$ git push origin :tagname
or
$ git push --delete origin tagname

Go module follow semantic versioning:

https://semver.org/
Go uses repository tags to lookup for versions.

Only version >= 2 will have different import path.
i.e
github.com/buddhavs/module_test  <= module base path, which contains 'util' package
import "github.com/buddhavs/module_test/util"  // for version 0 or 1
import "github.com/buddhavs/module_test/v2/util" // for version 2
import "github.com/buddhavs/module_test/v3/util" // for version 3


Steps for init new module:

1. 
$ go mod init github.com/buddhavs/module_test
Generates 'go.mod'
go.mod contains current projects path/version
i.e
module github.com/buddhavs/module_test  <= module base path, which contains 'util' package


2.
Import current module needed by the application.
$ go mod tidy
Generates updated go.mod and new go.sum

go commands other than go mod tidy no longer remove a require directive that specifies a version of an indirect dependency that is already implied by other (transitive) dependencies of the main module.
go commands other than go mod tidy no longer edit the go.mod file if the changes are only cosmetic.

3.
Series of git init commands:
$ git init
$ git add *   // includes go.mod
$ git commit -am "1 commit"
$ git push -u origin master

4. 
Anyone who uses the package can now do follow commands as before:
$ go get github.com/buddhavs/module_test

5.
$ git tag v1.0.0 // tag the version

6.
$ git checkout -b v1
$ git push -u origin v1 --tags

Steps for create new minor/patch version module:

$ git commit -m "patch version."
$ git tag v1.0.1
$ git push --tags origin v1.0.1

Steps for create new major version module:

$ git commit -m "v2"
git checkout -b v2 # Recommended.
$ echo "module github.com/buddhavs/module_test/v2" > go.mod
$ git commit go.mod -m "Bump to v2"
$ git tag v2.0.0
$ git push --tags origin v2.0.0 # or master if don't have a branch

Consume modules:

1.
If using module v0 or v1 with minor or patch, the import path is as usual.
i.e
package main
import mt "github.com/buddhavs/module_test"
func main() {
  mt.Run()
}


2.
If using module > v1, import path with version number.
i.e
package main
import mt "github.com/buddhavs/module_test/v2"  // it's mentioned in the module's go.mod
func main() {
  mt.Run()
}


3.
go build will use the latest version of the main version of the module.


Consume modules from local file path:

https://github.com/golang/go/wiki/Modules#can-i-work-entirely-outside-of-vcs-on-my-local-filesystem
Modify go.mod with 'replace' key word:
go.mod:
--
module module_main
require module_test/v2 v2.0.1
replace module_test/v2 => ../module_test
--

Cmd:

list current project's needed modules:
$ go list -m all
Fails explicitly if -mod=vendor is set and information is requested for a module not mentioned in vendor/modules.txt

find updated modules:
$ go list -m -u all

get all modules source code only:
$ go get -d

upgrade all modules:
$ go get -u

get the latest version:
go get -u github.com/vsdmars/project@latest

show module versions:
$ go list -m rsc.io/sampler

use 'go get' to upgrade/downgrade modules:
$ go get -u // use the latest minor or patch releases (i.e. it would update from 1.0.0 to, say, 1.0.1 or, if available, 1.1.0)

$ go get -u=patch // use the latest patch releases (i.e., would update to 1.0.1 but not to 1.1.0)

$ go get package@version //update to a specific version (say, github.com/robteix/testmod@v1.0.1)

use local changes:
$ go mod edit -replace 'rsc.io/quote=../quote'

use remote version:
$ go mod edit -replace 'rsc.io/quote=github.com/myitcv/london-gophers-quote-fork@v0.0.0-myfork'  // @TAG


Go Clean:

https://manpages.debian.org/testing/golang-go/go-clean.1.en.html
# -r clean to be applied recursively to all the dependencies of the packages named by the import paths.
$ go clean -r 


Go Module Proxy

Download Protocol

Reference: https://docs.gomods.io/intro/protocol/

go command determines needing a module, it first looks at the local cache
(under $GOPATH/pkg/mods ). If it can’t find the files there, it then goes from the network.

To control what files go can download, setting the GOPROXY environment variable to point to our proxy’s URL.

For instance:
$ export GOPROXY=http://gproxy.association.local:8080

The proxy is a web server that responds to the module download protocol, which is an API to query and fetch modules. The web server may serve static files.

( figure from https://roberto.selbach.ca/go-proxies/ )

We can set GOPROXY to local directory which has the correct directory layout of the API protocol also works:
$ export GOPROXY=file://home/robteix/devel/go-proxy-blog
Project Athens, a go module proxy project: https://github.com/gomods/athens


Go 1.12

https://golang.org/doc/go1.12#modules
Go Install doesn't work anymore if GO111MODULE=ON
There will be a work around on golang v1.12
https://github.com/golang/go/issues/24250

$ go run x.go
$ go get rsc.io/2fa@v1.1.0
Both can now(go v.1.12) operate in GO111MODULE=on mode without an explicit go.mod file.

Commands such as
  • go get
  • go list
  • go mod download
behave as if in a module with initially-empty requirements.
In this mode, go env GOMOD reports the system's null device (/dev/null or NUL).

The go directive in a go.mod file now indicates the version of the language used by the files within that module.

This changed use of the go directive means that if you use Go 1.12 to build a module, thus recording go 1.12 in the go.mod file, you will get an error when attempting to build the same module with Go 1.11 through Go 1.11.3. Go 1.11.4 or later will work fine, as will releases older than Go 1.11. If you must use Go 1.11 through 1.11.3, you can avoid the problem by setting the language version to 1.11, using the Go 1.12 go tool, via go mod edit -go=1.11

When an import cannot be resolved using the active modules, the go command will now try to use the modules mentioned in the main module's replace directives before consulting the module cache and the usual network sources.

If a matching replacement is found but the replace directive does not specify a version, the go command uses a pseudo-version derived from the zero time.Time (such as v0.0.0-00010101000000-000000000000).

Go 1.13

Reference:
  1. GO111MODULE environment variable continues to default to auto, but the auto setting now activates the module-aware mode of the go command whenever the current working directory contains, or is below a directory containing, a go.mod file — even if the current directory is within GOPATH/src.
  2. GOPATH will be deprecated in go v.1.13
  3. the GOPROXY (by default in 1.13) environment variable allows comma-separated list. It’ll try the first proxy before falling back to the next path.
  4. The default value of GOPROXY is set as https://proxy.golang.org,direct
    Anything after the direct token is ignored.
    If don’t want to use Go proxy , set it to off.
  5. A new GOPRIVATE environment variable is introduced, contains a comma-separated list of glob patterns.
    This can be used to bypass the GOPROXY proxy for certain paths,
    i.e private modules in a company
    (e.g: GOPRIVATE=*.internal.company.com).
  6. The new GOSUMDB environment variable identifies the name, and optionally the public key and server URL, of the database to consult for checksums of modules that are not yet listed in the main module's go.sum file.
    If GOSUMDB does not include an explicit URL, the URL is chosen by probing the GOPROXY URLs for an endpoint indicating support for the checksum database, falling back to a direct connection to the named database if it is not supported by any proxy.
    If GOSUMDB is set to off, the checksum database is not consulted and only the existing checksums in the go.sum file are verified.
  7. Users who cannot reach the default proxy and checksum database (for example, due to a firewalled or sandboxed configuration) may disable their use by setting GOPROXY to direct, and/or GOSUMDB to off. go env -w can be used to set the default values for these variables independent of platform:
    $ go env -w GOPROXY=direct
    $ go env -w GOSUMDB=off
  8. proxy list:
    1. https://proxy.golang.org
    2. https://goproxy.io
  9. opensource projects:
    1. https://github.com/gomods/athens
    2. https://github.com/goproxy/goproxy
    3. https://thumbai.app/


Go Module Index

Module Authentication

go command uses go.sum to verify that dependencies are bit-for-bit identical to the expected versions before using them in a build.

Module Discovery





Reference: