Getting a use of package without selector error

When you import a package like import “github.com/spf13/viper” the package name (which is viper in this case) will be available to you as a new identifier. You may use this identifier to construct qualified identifiers to refer to exported identifiers of the package (identifiers that start wtih an uppercase letter). The package name itself cannot … Read more

Go gin framework CORS

FWIW, this is my CORS Middleware that works for my needs. func CORSMiddleware() gin.HandlerFunc { return func(c *gin.Context) { c.Writer.Header().Set(“Access-Control-Allow-Origin”, “*”) c.Writer.Header().Set(“Access-Control-Allow-Credentials”, “true”) c.Writer.Header().Set(“Access-Control-Allow-Headers”, “Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With”) c.Writer.Header().Set(“Access-Control-Allow-Methods”, “POST, OPTIONS, GET, PUT”) if c.Request.Method == “OPTIONS” { c.AbortWithStatus(204) return } c.Next() } }

Is there any way to access private fields of a struct from another package?

There is a way to read unexported members using reflect (in Go < 1.7) func read_foo(f *Foo) { v := reflect.ValueOf(*f) y := v.FieldByName(“y”) fmt.Println(y.Interface()) } However, trying to use y.Set, or otherwise set the field with reflect will result in the code panicking that you’re trying to set an unexported field outside the package. … Read more

Convert an integer to a byte array

I agree with Brainstorm’s approach: assuming that you’re passing a machine-friendly binary representation, use the encoding/binary library. The OP suggests that binary.Write() might have some overhead. Looking at the source for the implementation of Write(), I see that it does some runtime decisions for maximum flexibility. func Write(w io.Writer, order ByteOrder, data interface{}) error { … Read more

How can I open files relative to my GOPATH?

Hmm… the path/filepath package has Abs() which does what I need (so far) though it’s a bit inconvenient: absPath, _ := filepath.Abs(“../mypackage/data/file.txt”) Then I use absPath to load the file and it works fine. Note that, in my case, the data files are in a package separate from the main package from which I’m running … Read more

Terminating a Process Started with os/exec in Golang

Run and terminate an exec.Process: // Start a process: cmd := exec.Command(“sleep”, “5”) if err := cmd.Start(); err != nil { log.Fatal(err) } // Kill it: if err := cmd.Process.Kill(); err != nil { log.Fatal(“failed to kill process: “, err) } Run and terminate an exec.Process after a timeout: ctx, cancel := context.WithTimeout(context.Background(), 3 * … Read more

Get exit code – Go

It’s easy to determine if the exit code was 0 or something else. In the first case, cmd.Wait() will return nil (unless there is another error while setting up the pipes). Unfortunately, there is no platform independent way to get the exit code in the error case. That’s also the reason why it isn’t part … Read more

Testing a gRPC service

I think you’re looking for the google.golang.org/grpc/test/bufconn package to help you avoid starting up a service with a real port number, but still allowing testing of streaming RPCs. import “google.golang.org/grpc/test/bufconn” const bufSize = 1024 * 1024 var lis *bufconn.Listener func init() { lis = bufconn.Listen(bufSize) s := grpc.NewServer() pb.RegisterGreeterServer(s, &server{}) go func() { if err … Read more

Is there a queue implementation?

In fact, if what you want is a basic and easy to use fifo queue, slice provides all you need. queue := make([]int, 0) // Push to the queue queue = append(queue, 1) // Top (just get next element, don’t remove it) x = queue[0] // Discard top element queue = queue[1:] // Is empty … Read more