Category Archives: golang
Embedding assets to go binary in go 1.16
That’s how you can use it: package server import «embed» // content holds our static web server content. //go:embed image/* template/* //go:embed html/index.html var content embed.FS data, _ := f.ReadFile(«html/index.html») print(string(data)) Or embedding one file directly into a variable: import _ «embed» //go:embed hello.txt var s string print(s) More details — https://golang.org/pkg/embed/
Forward multiple kubernetes pods for local development
kubefwd is a great tool that can forward a lot of pods at once to your local machine. The only downside for me is that it touches your local `/etc/hosts`. That’s how I use it: sudo KUBECONFIG=<path to your k8s config> kubefwd svc -l «app in (first-api, second-api, third-service)» It does not have huge documentation, …
Force ipv4 for golang http client
Sometimes you want to prevent your http client from using ipv6/tcp6. That’s how you do it. dialer := &net.Dialer{ Timeout: dialTimeout, KeepAlive: keepAliveTimeout, } transport := &http.Transport{ Proxy: http.ProxyFromEnvironment, ForceAttemptHTTP2: false, DialContext: func(ctx context.Context, network string, addr string) (net.Conn, error) { ipv4, err := resolveIPv4(addr) if err != nil { return nil, err } return …
Mocking for unit-tests and e2e-tests in golang
Some patterns to test your golang app. Mocking an interface mockS3 := &mocks.S3API{} mockResultFn := func(input *s3.ListObjectsInput) *s3.ListObjectsOutput { output := &s3.ListObjectsOutput{} output.SetCommonPrefixes([]*s3.CommonPrefix{ &s3.CommonPrefix{ Prefix: aws.String(«2017-01-01»), }, }) return output } // NB: .Return(…) must return the same signature as the method being mocked. // In this case it’s (*s3.ListObjectsOutput, error). mockS3.On(«ListObjects», mock.MatchedBy(func(input *s3.ListObjectsInput) bool …
Install go2 beta on MacOS
A quick guide on installing golang v2 development revision. It works for my MacOS Big Sur (with an Intel processor).
Using DataDog on your CentOs
DD_AGENT_MAJOR_VERSION=7 DD_API_KEY=<your-key> DD_SITE=»datadoghq.eu» bash -c «$(curl -L https://s3.amazonaws.com/dd-agent/scripts/install_script.sh)» sudo nano /etc/datadog-agent/datadog.yaml # uncomment logs_enabled: true sudo systemctl restart datadog-agent sudo mkdir /etc/datadog-agent/conf.d/go.d sudo nano /etc/datadog-agent/conf.d/go.d/conf.yaml # and add contents from the url below https://app.datadoghq.eu/logs/onboarding/server https://docs.datadoghq.com/agent/faq/error-restarting-agent-already-listening-on-a-configured-port/ Or, if you want a dockerized version of DataDog agent, and you want to listen to docker logs, you can …
Install go with gvm on MacOS Big Sur
I used to use gvm for this — https://github.com/moovweb/gvm. But initial installation of go from scratch does not work for Big Sur as 1.4 does not have a binary for this OS (this command fails — gvm install go1.4 -B). To use gvm, you can do this: 0. Install gvm — bash <
Complete list of swagger options to protobuf file
Here is an example with many options that help generate proper swagger out of protofile. Original URL — https://raw.githubusercontent.com/grpc-ecosystem/grpc-gateway/master/examples/internal/proto/examplepb/a_bit_of_everything.proto.
Refactor your code and know it’s completely backward compatible
The idea is to run 2 versions of code on production: 1 (control group) — is your old implementation which will be returned to the end user 2 (the experiment) — is a new implementation which results will be checked against the control group and logged It will be run asynchronously and only for a …
GRPC fallback to Rest API with custom field names
When you generate JSON for Rest API from proto-file, protoc-gen-gofast generates field names for JSON in lowerCamelCase format while most of the Rest APIs use snake_case for that. And if you want to replace some legacy API with your new implementation without breaking backward compatibility, you need to fix it. There could be different ways …