Go build multiple/nested packages?

After you cd to the base directory, use go build ./… Note that there are 3 periods as it is an ellipsis. This will recursively build all subdirectories. Of course you can always do go build path/to/my/base/… from wherever without needing to cd to the directory. This is very useful for those who use an … Read more

What’s the difference between errors.Wrapf(), errors.Errorf(), and fmt.Errorf()?

First, a correction: github.com/pkg/errors is not part of the standard library! The standard errors package has a much smaller API. That said, github.com/pkg/errors is very popular, and was* maintained by some prominent Gophers. It is, however, largely (though not completely**) obsoleted by Go 1.13’s extended error support. Understanding the difference between those three functions requires … Read more

Tour of Go exercise #10: Crawler

A look at the Parallelization section of Effective Go leads to ideas for the solution. Essentually you have to close the channel on each return route of the function. Actually this is a nice use case of the defer statement: func Crawl(url string, depth int, fetcher Fetcher, ret chan string) { defer close(ret) if depth … Read more

How to use Delve debugger in Visual Studio Code

For using Delve debugger in Visual Studio Code with Golang, do the following steps: ( Note: for Windows OS replace all $GOPATH with %GOPATH% ) Install Latest Golang and set GOROOT and GOPATH Add $GOPATH/bin to your OS PATH environment variable. set environment variable: GO15VENDOREXPERIMENT = 1 run: go get github.com/derekparker/delve/cmd/dlv and make sure dlv … Read more

How to set vscode format golang code on save?

For me, none of the answers worked. My Go version is 1.17.1, VSCode version is 1.60.1 and I’m using Linux Pop!_os. After some digging online found this in the official VSCode documentation for Go. https://code.visualstudio.com/docs/languages/go#_formatting My settings.json looks like this “[go]”: { “editor.insertSpaces”: true, “editor.formatOnSave”: true, “editor.defaultFormatter”: “golang.go” }, Note: You need to install the … Read more

Go/GoLang check IP address in range

IP addresses are represented as bigendian []byte slices in go (the IP type) so will compare correctly using bytes.Compare. Eg (play) package main import ( “bytes” “fmt” “net” ) var ( ip1 = net.ParseIP(“216.14.49.184”) ip2 = net.ParseIP(“216.14.49.191”) ) func check(ip string) bool { trial := net.ParseIP(ip) if trial.To4() == nil { fmt.Printf(“%v is not an … Read more

How to download file in browser from Go server

To make the browser open the download dialog, add a Content-Disposition and Content-Type headers to the response: w.Header().Set(“Content-Disposition”, “attachment; filename=WHATEVER_YOU_WANT”) w.Header().Set(“Content-Type”, r.Header.Get(“Content-Type”)) Do this BEFORE sending the content to the client. You might also want to copy the Content-Length header of the response to the client, to show proper progress. To stream the response body … Read more

Is there analog of memset in go?

The simplest solution with a loop would look like this: func memsetLoop(a []int, v int) { for i := range a { a[i] = v } } There is no memset support in the standard library, but we can make use of the built-in copy() which is highly optimized. With repeated copy() We can set … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)