golang

module
v0.0.0-...-2eefbb4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2021 License: MIT

README ΒΆ

Golang

Golang Interactive Playground

Let' get start.

πœ† git clone https://github.com/TerrenceMiao/golang src

πœ† go mod init github.com/TerrenceMiao/golang

A file go.mod is created:

πœ† cat go.mod
module github.com/TerrenceMiao/golang

go 1.16

Now run the command:

πœ† bazel run //:gazelle 

which tells bazel to run the gazelle target specified in the BUILD file. This will autogenerate the BUILD.bazel files for all of the packages.

πœ† tree -C
.
β”œβ”€β”€ BUILD
β”œβ”€β”€ LICENSE
β”œβ”€β”€ README.md
β”œβ”€β”€ WORKSPACE
β”œβ”€β”€ bazel
β”‚Β Β  β”œβ”€β”€ docker
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ BUILD
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ def.bzl
β”‚Β Β  β”‚Β Β  └── repos.bzl
β”‚Β Β  └── go
β”‚Β Β      β”œβ”€β”€ BUILD
β”‚Β Β      β”œβ”€β”€ WORKSPACE
β”‚Β Β      β”œβ”€β”€ def.bzl
β”‚Β Β      └── repos.bzl
β”œβ”€β”€ bazel-bin -> /private/var/tmp/_bazel_terrence/3...6/execroot/__main__/bazel-out/darwin-fastbuild/bin
β”œβ”€β”€ bazel-out -> /private/var/tmp/_bazel_terrence/3...6/execroot/__main__/bazel-out
β”œβ”€β”€ bazel-src -> /private/var/tmp/_bazel_terrence/3...6/execroot/__main__
β”œβ”€β”€ bazel-testlogs -> /private/var/tmp/_bazel_terrence/3...6/execroot/__main__/bazel-out/darwin-fastbuild/testlogs
β”œβ”€β”€ go.mod
β”œβ”€β”€ go_third_party.bzl
β”œβ”€β”€ link_go.sh
β”œβ”€β”€ protos
β”‚Β Β  β”œβ”€β”€ common
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ BUILD.bazel
β”‚Β Β  β”‚Β Β  └── common.proto
β”‚Β Β  └── hello
β”‚Β Β      β”œβ”€β”€ BUILD.bazel
β”‚Β Β      β”œβ”€β”€ hello.proto
β”‚Β Β      └── hello_service.proto
└── services
    └── hello
        β”œβ”€β”€ BUILD.bazel
        β”œβ”€β”€ main.go
        └── server
            β”œβ”€β”€ BUILD.bazel
            β”œβ”€β”€ server.go
            └── server_test.go

13 directories, 24 files

Now, inform bazel about the dependencies mentioned in go.mod file. Either:

πœ† go get github.com/bazelbuild/bazel-gazelle/cmd/gazelle
πœ† gazelle -go_prefix github.com/TerrenceMiao/golang
πœ† gazelle update-repos --from_file=go.mod -to_macro=go_third_party.bzl%go_deps

or, with bazel:

πœ† bazel run //:gazelle -- update-repos -from_file=go.mod -to_macro=go_third_party.bzl%go_deps

Compile hello service:

πœ† bazel build //services/hello
INFO: Analyzed target //services/hello:hello (117 packages loaded, 1553 targets configured).
INFO: Found 1 target...
Target //services/hello:hello up-to-date:
  bazel-bin/services/hello/hello_/hello
INFO: Elapsed time: 2.331s, Critical Path: 0.06s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action

Run hello service, with default proxy port 24689:

πœ† bazel run //services/hello
INFO: Analyzed target //services/hello:hello (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //services/hello:hello up-to-date:
  bazel-bin/services/hello/hello_/hello
INFO: Elapsed time: 0.430s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
2021/07/17 20:04:25 Setting proxy server port 24689

Run hello service, with proxy port 8082:

πœ† bazel run //services/hello -- --proxy-port 8082
INFO: Analyzed target //services/hello:hello (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //services/hello:hello up-to-date:
  bazel-bin/services/hello/hello_/hello
INFO: Elapsed time: 0.538s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
2021/07/17 20:06:03 Setting proxy server port 8082

Publishing a module

Removes any dependencies the module might have accumulated that are no longer necessary.

πœ† go mod tidy
warning: ignoring symlink /Users/terrence/Projects/golang/src/bazel-bin
warning: ignoring symlink /Users/terrence/Projects/golang/src/bazel-out
warning: ignoring symlink /Users/terrence/Projects/golang/src/bazel-src
warning: ignoring symlink /Users/terrence/Projects/golang/src/bazel-testlogs
go: finding module for package github.com/TerrenceMiao/golang/protos/hello
go: finding module for package github.com/jessevdk/go-flags
go: finding module for package github.com/TerrenceMiao/golang/protos/common
go: finding module for package google.golang.org/grpc
go: finding module for package github.com/stretchr/testify/require
go: found github.com/jessevdk/go-flags in github.com/jessevdk/go-flags v1.5.0
go: found google.golang.org/grpc in google.golang.org/grpc v1.39.0
go: found github.com/stretchr/testify/require in github.com/stretchr/testify v1.7.0
go: downloading golang.org/x/net v0.0.0-20200822124328-c89045814202
go: downloading github.com/golang/protobuf v1.4.3
go: downloading gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405
go: downloading google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013
go: downloading github.com/google/go-cmp v0.5.0
go: downloading google.golang.org/protobuf v1.25.0

Tag the project with a version number.

πœ† git tag -a 1.0.0 -m "Publish module version 1.0.0"

πœ† git push origin 1.0.0
Enumerating objects: 1, done.
Counting objects: 100% (1/1), done.
Writing objects: 100% (1/1), 829 bytes | 829.00 KiB/s, done.
Total 1 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/TerrenceMiao/golang.git
 * [new tag]         1.0.0 -> 1.0.0

Publish Golang module.

πœ† env GOPROXY=proxy.golang.org go list -m github.com/TerrenceMiao/golang@1.0.0
github.com/TerrenceMiao/golang v0.0.0-20210717101020-6b3b278957f1

Can find the published Golang module at https://pkg.go.dev/github.com/TerrenceMiao/golang

Reference

Directories ΒΆ

Path Synopsis
services

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL