Latest Posts in Golang

Browse latst posts in Golang Category

Published 14 Apr, 2022
How to do Unit Testing with gorm

"How to unit test" is a very broad question since it depends on what you want to test. In your examp...

Published 12 Mar, 2022
How to check for an empty struct?

How to check for an empty struct?

Published 04 May, 2022
How to Mock only specific method in Golang

You can't really override methods in golang as per this answer. However, as you point out you can ha...

Published 05 May, 2022
How to list installed go packages

goinstall is now history goinstall was replaced by go get. go get is used to manage external / 3rd party libraries (e.g. to download them, update them, install them etc). Type go help get to see com...

Published 11 Apr, 2022
How to cast/convert a Struct to Protobuf?

You cannot, at least not in Go 1.12.7. Go's Protobuf compiler adds 3 extra fields to each struct gen...

Published 01 May, 2022
How to filter date with golang and mongodb?

Try primitive.NewObjectIDFromTimestamp. From there you can convert time.Time to primitive.ObjectID a...

Published 05 May, 2022
How To Retrieve The Current Function Name in Golang?

Often times you need to retrieve the name of the function that was called and being executed from within that function. You may use this keyword to find it out.

Published 07 May, 2022
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/lestrr...

Published 15 Apr, 2022
Golang: A map Interface, how to print key and value

The OP's comment on the question states that type of getUsersAppInfo is []map[string]interface{}. Lo...

Published 09 Apr, 2022
How to set the expiry of the redis keys in golang

Redis documentation does not support a command like "HMSETEX". "HMSET" modifies the hashkeys and not...

Published 09 Apr, 2022
How to convert interface{} to map

If the map value can be any type, then use reflect to iterate through the map: if v.Kind() == reflec...

Published 10 Apr, 2022
How to scan a QueryRow into a struct with pgx

Not with plain database/sql but there is an extension library called sqlx that builds on top of data...

Published 04 May, 2022
How to set memory limit to a process in Golang

RLIMIT_DATA describes the maximum size of a processes data segment. Traditionally, programs that all...

Published 20 Apr, 2022
How to get all query parameters from go *gin.context object

You should be able to do c.Request.URL.Query() which will return a Values which is a map[string][]st...

Published 12 Apr, 2022
How to have a global variable accessible across all packages

declare a variable at the top level - outside of any functions: var Global = "myvalue" func InitApp(...

Published 10 Apr, 2022
How to ignore generated files from Go test coverage

Most Go tools operate on packages, because a package itself forms a unit that may be useful in its e...

Published 23 Apr, 2022
How to insert a json object array to mongodb in golang

Unmarshal the JSON to []interface{} and insert the result in the database. Assuming that c is an mgo...

Published 22 Apr, 2022
How to get header data of postman using gin package in golang?

You can get the token header with c.Request.Header["Token"]. Here is a sample code. package main imp...

Published 17 Apr, 2022
How to iterate over an []interface{} in Go

If you know that's your data structure, there's no reason to use reflection at all. Just use a type...

Published 27 Apr, 2022
How to specify default values when parsing JSON in Go

This is possible using encoding/json: when calling json.Unmarshal, you do not need to give it an emp...