Category Archives: golang
Go library for creating slack bots
You can use it like that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package main import ( "context" "github.com/shomali11/slacker" "log" ) func main() { bot := slacker.NewClient("<YOUR SLACK BOT TOKEN>") definition := &slacker.CommandDefinition{ Handler: func(request slacker.Request, response slacker.ResponseWriter) { response.Reply("pong") }, } bot.Command("ping", definition) ctx, cancel := context.WithCancel(context.Background()) defer cancel() err := bot.Listen(ctx) if err != nil { log.Fatal(err) } } |
https://github.com/shomali11/slacker
How to work with core dumps of Go programs
If you want to check what is going on in your service during load (on production or during stress testing process), you can take a core dump of your app and load it into Goland for further debugging.
1 2 3 4 5 6 |
# first you need to get your app's PID ps aux|grep <your-app-bin-name> gcore <PID> # download dump file and binary file to a local machine with kubectl cp <some-namespace>/<some-pod>:/core.<PID> ~/Downloads kubectl cp <some-namespace>/<some-pod>:<your-app-bin-path> ~/Downloads |
Then open it your Goland: Navigate to Run | Open Core Dump. In the Executable field, …
Nice introductory article on why Golang is not OOP
It is a copy of this article — https://rakyll.org/typesystem/ It is real struggle to work with a new language, especially if the type doesn’t resemble what you have previously seen. I have been there with Go and lost my interest in the language when it first came out due to the reason I was pretending …
A way to test http client in go
A couple of ways are described here — http://hassansin.github.io/Unit-Testing-http-client-in-Go The way I liked is the following one:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
func Test_Mine(t *testing.T) { ... client := httpClientWithRoundTripper(http.StatusOK, "OK") ... } type roundTripFunc func(req *http.Request) *http.Response func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { return f(req), nil } func httpClientWithRoundTripper(statusCode int, response string) *http.Client { return &http.Client{ Transport: roundTripFunc(func(req *http.Request) *http.Response { return &http.Response{ StatusCode: statusCode, Body: ioutil.NopCloser(bytes.NewBufferString(response)), } }), } } |
Go internals
Channels https://youtu.be/KBZlN0izeiY Go scheduler https://youtu.be/YHRO5WQGh0k
Setup local Athens for proper go mod
There is a project called Athens for proxying all your go mod downloads. How to use it locally. 1. Create the following files and directories: a. /Users/d.bolgov/.ssh-athens/storage — empty directory to cache go modules b. /Users/d.bolgov/.ssh-athens/.netrc — if you prefer using .netrc
1 2 3 |
machine github.com login [yourlogin] password [yourpassword] |
c. /Users/d.bolgov/.ssh-athens/gitconfig/.gitconfig — if you want to substitue some urls. I personally …
Installing protobuf tools on MacOS
Installing protoc
1 |
brew install protobuf |
Or follow different instructions. Installing grpc_cli Option 1. Easy way.
1 2 |
brew tap grpc/grpc brew install --with-plugins grpc |
It is described here — https://github.com/grpc/homebrew-grpc. Option 2. Hard way — using cmake and make. NOT RECOMMENDED.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
brew install autoconf automake libtool shtool cmake git clone -b $(curl -L https://grpc.io/release) https://github.com/grpc/grpc cd grpc git submodule update --init mkdir -p cmake/build cd cmake/build cmake ../.. make gRPC_INSTALL=ON make install # and continue accordgin to https://github.com/grpc/grpc/blob/master/BUILDING.md |
Or follow these instructions
Nice talk about concurrency in Go
Another good talk. It’s not only about channels, but also about atomics and patterns around ti. https://www.youtube.com/watch?v=YEKjSzIwAdA
Make your bot for telegram using go
It’s really not that difficult. Here are the docs: https://core.telegram.org/bots https://core.telegram.org/bots/api Here is a simple library in go for telegram — https://github.com/go-telegram-bot-api/telegram-bot-api/ (too simple, from my point of view, does not cover all functionality, but okay). And here is a skeletton for making your bots if you want it as just standalone binary — https://github.com/nezorflame/example-telegram-bot/
Gitlab-ci: build go app with docker as a docker image
Preparations [TO BE UPDATED LATER] My gitlab-ci.yml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
variables: PACKAGE_PATH: /go/src/gitlab.com/[username_for_gitlab]/[project_name] # https://docs.gitlab.com/ee/ci/docker/using_docker_build.html CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest stages: - dep - test - build # A hack to make Golang-in-Gitlab happy .anchors: - &inject-gopath mkdir -p $(dirname ${PACKAGE_PATH}) && ln -s ${CI_PROJECT_DIR} ${PACKAGE_PATH} && cd ${PACKAGE_PATH} dep: stage: dep image: golang:1.13.1-alpine before_script: - *inject-gopath script: go mod tidy && go mod vendor artifacts: name: "vendor-$CI_PIPELINE_ID" paths: - vendor/ expire_in: 1 hour test: stage: test dependencies: - dep image: docker:17 services: - docker:dind before_script: - *inject-gopath script: docker build --target=test . lint: stage: test dependencies: - dep image: docker:17 services: - docker:dind before_script: - *inject-gopath script: docker build --target=lint . build: stage: build dependencies: - dep - lint - test image: docker:17 services: - docker:dind before_script: - *inject-gopath - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY registry script: - docker build --target=release --pull -t $CONTAINER_TEST_IMAGE . - docker tag $CONTAINER_TEST_IMAGE $CONTAINER_RELEASE_IMAGE - docker push $CONTAINER_TEST_IMAGE |
My Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
FROM golang:1.13.1-alpine AS build WORKDIR /go/src/gitlab.com/[username_for_gitlab]/[project_name] COPY . . RUN go get -u github.com/jessevdk/go-assets-builder RUN apk add --no-cache make RUN make build #some pareparations and after that: go build -i -o ./[path_to_bin] ./cmd/[...] FROM alpine:3.10 AS release COPY --from=build /go/src/gitlab.com/[username_for_gitlab]/[project_name]/[path_to_bin] /usr/local/bin/[appname] RUN apk add --no-cache make bash curl tzdata EXPOSE 3000 3001 ENTRYPOINT ["/usr/local/bin/[appname]"] FROM golang:1.13.1-alpine AS test ENV CGO_ENABLED=0 WORKDIR /go/src/gitlab.com/[username_for_gitlab]/[project_name] COPY . . RUN apk add --no-cache make bash RUN make test FROM golangci/golangci-lint:v1.20 AS lint WORKDIR /go/src/gitlab.com/[username_for_gitlab]/[project_name] COPY . . RUN make lint-full # to check things locally # docker build --target=check_container . # docker images # docker run -it <hash> /bin/bash FROM golang:1.13.1-alpine AS check_container WORKDIR /go/src/gitlab.com/[username_for_gitlab]/[project_name] COPY . . RUN apk add --no-cache make bash RUN /bin/bash FROM release |
Pulling from your private registry Create deploy token as described here. After that, you can do this:
1 2 3 |
docker login -u [deploy_username] -p [deploy_token] registry.gitlab.com docker pull registry.gitlab.com/[username_for_gitlab]/[project_name]:master docker run -d --name [container_name_you_like] --env-file [path-to-.env] --net host --rm registry.gitlab.com/[username_for_gitlab]/[project_name]:master |
Useful links: https://dev.to/hypnoglow/how-to-make-friends-with-golang-docker-and-gitlab-ci-4bil https://docs.gitlab.com/ee/ci/docker/using_docker_build.html https://medium.com/@tonistiigi/advanced-multi-stage-build-patterns-6f741b852fae https://docs.gitlab.com/ee/ci/yaml/ (full description of gitlab-ci.yml) https://about.gitlab.com/blog/2017/11/27/go-tools-and-gitlab-how-to-do-continuous-integration-like-a-boss/ https://gitlab.com/hypnoglow/example-go-docker-gitlab/blob/master/.gitlab-ci.yml Somewhat useful: https://blog.lwolf.org/post/how-to-build-tiny-golang-docker-images-with-gitlab-ci/ https://angristan.xyz/build-push-docker-images-gitlab-ci/ https://docs.gitlab.com/ee/user/packages/container_registry/#use-images-from-gitlab-container-registry https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-centos-7 (it says to do sudo usermod -aG docker …