Golang and Kubernetes

How to import 'any' Kubernetes package into your project?

The client libraries that Kubernetes ships are meant to be imported, and you definitely don’t need this post explaining how to import them in your Golang based project. A simple go get ... should do the trick. But, what about the packages that are not meant to be imported? Or the ones that cannot be imported because of “technical reasons” ? Could you simply add them to your import statements in the ....

May 30, 2021 Â· 5 min Â· Suraj Deshmukh

Cobra and Persistentflags gotchas

If you are using cobra cmd line library for golang applications and it’s PersistentFlags and if you have a use case where you are adding same kind of flag in multiple places. You might burn your fingers in that case, if you keep adding it in multiple sub-commands without giving it a second thought. To understand what is really happening and why it is happening follow along. All the code referenced here can be found here https://github....

January 4, 2019 Â· 3 min Â· Suraj Deshmukh

Golang struct tags gotchas

In golang while using struct tag, the spaces make a lot of difference. For example look at the following code. type PodStatus struct { Status string `json: ",status"` } If you run go vet on this piece of code you will get following error: $ go vet types.go # command-line-arguments ./types.go:28: struct field tag `json: ",status"` not compatible with reflect.StructTag.Get: bad syntax for struct tag value Now this does not tell us what is wrong with the struct tag, json: ",status"....

August 12, 2018 Â· 1 min Â· Suraj Deshmukh

Notes on talk - Advanced testing in golang by Mitchell Hashimoto

Test Fixtures “go test” sets pwd as package directory Test Helpers should never return an error they should access to the *testing.T object call t.Helper() in the beginning (works only for go1.9+) for things reqiuiring clean up return closures Configurability Unconfigurable behavior is often a point of difficulty for tests. e.g. ports, timeouts, paths. Over-parameterize structs to allow tests to fine-tune their behavior It’s ok to make these configs unexported so only tests can set them....

March 7, 2018 Â· 1 min Â· Suraj Deshmukh

Methods that satisfy interfaces in golang

Pointer receiver For a struct User with a method Work with pointer receiver. type User struct { Name string Period int } func (u *User) Work() { fmt.Println(u.Name, "has worked for", u.Period, "hrs.") } func main() { uval := User{"UserVal", 5} uval.Work() pval := &User{"UserPtr", 6} pval.Work() } See on go playground. output: UserVal has worked for 5 hrs. UserPtr has worked for 6 hrs. If we call this method on value type object uval it works, and obviously it works with pointer type object pval....

February 23, 2018 Â· 3 min Â· Suraj Deshmukh