Category "go"

Executing function upon required number of times

calculate the sum of squares of given integers, excluding any negatives. The first line of the input will be an integer N (1 <= N <= 100), indicating the

Marshal into a bson.Raw

Using gopkg.in/mgo.v2/bson, I wonder how to marshal an interface{} value into a value of type bson.Raw. The documentation for bson.Raw states: Using this t

How to debug Go tests with param "./..." in VS Code

I usually run go test ./... to run all tests in my project. How can I set up launch.json to debug every tests that normally go test ./... runs?

How to analyze golang memory?

I wrote a golang program, that uses 1.2GB of memory at runtime. Calling go tool pprof http://10.10.58.118:8601/debug/pprof/heap results in a dump with only 323

Golang : Is conversion between different struct types possible?

Let's say I have two similar types set this way : type type1 []struct { Field1 string Field2 int } type type2 []struct { Field1 string Field2 i

golang - How to convert byte slice to bool?

I have a database sql.NullBool. To unmarshal json into it, I am writing this little function. I can converty the byte array to string by simply casting it (stri

Dynamically initialize array size in go

I try to write a small application in go that takes 'x' numbers of integers from standard input, calculates the mean and gives it back. I have only gotten so fa

Creating yaml list using the go template

I am looking to create a yaml file in the following format. Using Go template Custom_listeners: Config1 : config_value Config2 : config_value

What should be the values of GOPATH and GOROOT?

I'm trying to install doozer like this: $ goinstall github.com/ha/doozer I get these errors. goinstall: os: go/build: package could not be found locally g

How to initialize a struct which has a pointer-type property?

The struct is like this: type Node struct { Stat *Stat } type Stat struct { StatInfo []string } Initialize the structure, and set properties. node :=

Range on Unbuffered chan with go routines throws all goroutines are asleep - deadlock [duplicate]

I have written a sample program to do file search, it does the search but fails at the end with fatal error: all goroutines are asleep - deadl

Golang Chromedp reload current page until find some html object

chromedp.Navigate(tragetUrl), chromedp.WaitVisible("#button"), chromedp.Click("#button"), Goal: if #button is not exist then reload the current page until butt

How to pass context in golang request to middleware

I am trying to understand how the context introduced in Golang 1.7 works and what would be the appropriate way to pass it to middleware and to a HandlerFunc. Sh

One type is coerced into another, can a method to determine the type of the receiver?

If types T1 and T2 are based on type T, and type T only comes into existence from a NewT1() or NewT2(), is there any way a function func (*T) WhoAmI() can know

How to prevent go mod tidy from looking up a replaced module path

Consider the following setup: go.mod module example.com/main require example.com/sub dummy replace example.com/sub => ./sub sub/go.mod module example.com/s

Cache "go get" in docker build

I want to encapsulate my golang unit testing in a docker-compose script because it depends on several external services. My application has quite a lot of depen

Terratest - How to pass Terraform outputs between functions

I tried to use 'test_structure.SaveTerraformOptions' but it doesn't save resources IDs. For example, I'm running 2 modules - Module 1 creating the networking an

Go closure variable scope

I'm reading 'CreateSpace An Introduction to Programming in Go 2012' and on page 86 I found this evil magic func makeEvenGenerator() func() uint { i := uin

How to transform [4]byte{1,2,3,4} to "1.2.3.4" in Golang?

Here is my code which was used to transfer a byte array to string but it was failed: package main import ( "bytes" "fmt" ) type IPAddr [4]byte // TO

Go << and >> operators

Could someone please explain to me the usage of << and >> in Go? I guess it is similar to some other languages.