New answers tagged

1

There is a base case... it's the line if x > 0 { compare to the for loop version for x=input: x>0: x=x-1 { findSquareSum(input,a,x) sets x=input if x > 0 { is the condition check findSquareSum(x-1, a, iteration) decrements x also consider the modified fucntion func findSquareSum(x int, a []int, iteration int) { var num int if x <= 0 { ...


1

I think the code could benefit a struct for the database instead of global state. You can use a New method instead of init() where you pass in the params, which would differet from test and production. I also demonstrated that you can keep the connection open and reuse it within the struct. If you prefer a lazy approach, you can omit this and create a the ...


0

The Go standard library strings package has a Join function: func Join(elems []string, sep string) string Your function is a simple extension of strings.Join: func JoinLast(elems []string, sep, lastSep string) string However, for no obvious reason, you chose a different function parameter signature: func JoinLast(sep string, lastSep string, words ...string)...


1

"Actually you code is pretty much this. Simple and boring as most other good Go code" I thought. At this point I could say "Wrap your c <- true with a select statement, add the third case <-ctx.Done: to handle possible deadlock and you are ready to go". But. Does it really should be this way? Yep, this works and does the job, but ...


2

The code is ignoring the return values from the go routine The WorkGroup isn't used to block until completion // 1. Perhaps you'd want to have a separate channel to communicate the errors. var errCh := make(chan error, len(charts)) for _, chartInstallation := range charts { wg.Add(1) go func(errCh chan error){ installed, err := installChart(&...


2

When it comes to "best practices" to mock interfaces, there's a number of things to consider, not in the least: ease of use. Over the years, I've taken to use a mock generator tool. Rather than just implementing the interface in question, GoMock supports a lot of other useful things. You can read more about it here. The way to use it in your case ...


0

One approach that's simple, but is a bit of a dirty trick: first replace sequences of escape+separator with a string that's never going to occur in your text (for example a NUL byte "\x00"), then do the split, then do the reverse replace on each token. For example (Go Playground link): func SplitWithEscaping(s, separator, escape string) []string { ...


2

This seems fine to me: it's nice and simple, and I've seen this pattern used "in the wild" before. One thing you could do is, in the MockUserRepo proxying functions, if the function variable is nil, either execute a default implementation, or return a more specific error (or even panic). For example: func (m *MockUserRepo) GetUserByEmail(s string) (...


Top 50 recent answers are included