Big O of append in Golang

This all depends on the actual implementation used, but I’m basing this on the standard Go as well as gccgo. Slices Reslicing means changing an integer in a struct (a slice is a struct with three fields: length, capacity and pointer to backing memory). If the slice does not have sufficient capacity, append will need … Read more

Build Docker Image From Go Code

The following works for me; package main import ( “archive/tar” “bytes” “context” “io” “io/ioutil” “log” “os” “github.com/docker/docker/api/types” “github.com/docker/docker/client” ) func main() { ctx := context.Background() cli, err := client.NewEnvClient() if err != nil { log.Fatal(err, ” :unable to init client”) } buf := new(bytes.Buffer) tw := tar.NewWriter(buf) defer tw.Close() dockerFile := “myDockerfile” dockerFileReader, err := … Read more

Force priority of go select statement

The accepted answer has a wrong suggestion: func sendRegularHeartbeats(ctx context.Context) { ticker := time.NewTicker(time.Second) defer ticker.Stop() for { //first select select { case <-ctx.Done(): return default: } //second select select { case <-ctx.Done(): return case <-ticker.C: sendHeartbeat() } } } This doesn’t help, because of the following scenario: both channels are empty first select runs … Read more

Do buffered channels maintain order?

“Are you guaranteed that B will read data In the same order that A put it into the channel?” Yes. The order of data is guaranteed. But the delivery is guaranteed only for unbuffered channels, not buffered. (see second section of this answer) You can see the idea of channels illustrated in “The Nature Of … Read more

How to disable “use of internal package not allowed”

An import of a path containing the element “internal” disallowed if the importing code is outside the tree rooted at the parent of the “internal” directory. There is no mechanism for exceptions. In particular, by design, there is no ACL mechanism for allowing a whitelist of other packages to use an internal package. Proposal for … Read more

How to verify JWT signature with JWK in Go?

Below is an example of JWT decoding and verification. It uses both the jwt-go and jwk packages: package main import ( “errors” “fmt” “github.com/dgrijalva/jwt-go” “github.com/lestrrat-go/jwx/jwk” ) const token = `eyJhbGciOiJSUzI1NiIsImtpZCI6Ind5TXdLNEE2Q0w5UXcxMXVvZlZleVExMTlYeVgteHlreW1ra1h5Z1o1T00ifQ.eyJzdWIiOiIwMHUxOGVlaHUzNDlhUzJ5WDFkOCIsIm5hbWUiOiJva3RhcHJveHkgb2t0YXByb3h5IiwidmVyIjoxLCJpc3MiOiJodHRwczovL2NvbXBhbnl4Lm9rdGEuY29tIiwiYXVkIjoidlpWNkNwOHJuNWx4ck45YVo2ODgiLCJpYXQiOjE0ODEzODg0NTMsImV4cCI6MTQ4MTM5MjA1MywianRpIjoiSUQuWm9QdVdIR3IxNkR6a3RUbEdXMFI4b1lRaUhnVWg0aUotTHo3Z3BGcGItUSIsImFtciI6WyJwd2QiXSwiaWRwIjoiMDBveTc0YzBnd0hOWE1SSkJGUkkiLCJub25jZSI6Im4tMFM2X1d6QTJNaiIsInByZWZlcnJlZF91c2VybmFtZSI6Im9rdGFwcm94eUBva3RhLmNvbSIsImF1dGhfdGltZSI6MTQ4MTM4ODQ0MywiYXRfaGFzaCI6Im1YWlQtZjJJczhOQklIcV9CeE1ISFEifQ.OtVyCK0sE6Cuclg9VMD2AwLhqEyq2nv3a1bfxlzeS-bdu9KtYxcPSxJ6vxMcSSbMIIq9eEz9JFMU80zqgDPHBCjlOsC5SIPz7mm1Z3gCwq4zsFJ-2NIzYxA3p161ZRsPv_3bUyg9B_DPFyBoihgwWm6yrvrb4rmHXrDkjxpxCLPp3OeIpc_kb2t8r5HEQ5UBZPrsiScvuoVW13YwWpze59qBl_84n9xdmQ5pS7DklzkAVgqJT_NWBlb5uo6eW26HtJwHzss7xOIdQtcOtC1Gj3O82a55VJSQnsEEBeqG1ESb5Haq_hJgxYQnBssKydPCIxdZiye-0Ll9L8wWwpzwig` const jwksURL = `https://companyx.okta.com/oauth2/v1/keys` func getKey(token *jwt.Token) (interface{}, error) { // TODO: cache response so we don’t have to make a request every time // … Read more

Call a Struct and its Method by name in Go?

To call a method on an object, first use reflect.ValueOf. Then find the method by name, and then finally call the found method. For example: package main import “fmt” import “reflect” type T struct {} func (t *T) Foo() { fmt.Println(“foo”) } func main() { var t T reflect.ValueOf(&t).MethodByName(“Foo”).Call([]reflect.Value{}) }

How to start a Go program as a daemon in Ubuntu?

You should build an executable for your program (go build) and then either write a script for upstart and it will run your program as a daemon for you, or use an external tool like daemonize. I prefer the latter solution, because it does not depend on a system-dependent upstart. With daemonize you can start … Read more