Category "go"

Golang custom sort is faster than native sort

I was just playing around with sorting in golang and I found a qsort function on stackoverflow. It seems to run about twice as fast as the native sort function

Unmarshalling json to structure using json.RawMessage

I need to unmarshal json object which may have the following formats: Format1: { "contactType": 2, "value": "0123456789" } Format2: { "contactType"

How to get the last element of a slice?

What is the Go way for extracting the last element of a slice? var slice []int slice = append(slice, 2) slice = append(slice, 7) slice[len(slice)-1:][0] // R

How to search for an element in a golang slice

I have a slice of structs. type Config struct { Key string Value string } // I form a slice of the above struct var myconfig []Config // unmarshal a

Does golang have the plan for supporting set datastructure?

This feature can be implemented with 'map'. countrySet := map[string]bool{ "US": true, "JP": true, "KR": true, } But to ease the eyes of readers, 'set'

is concurrent write on stdout threadsafe?

below code does not throw a data race package main import ( "fmt" "os" "strings" ) func main() { x := strings.Repeat(" ", 1024) go func()

What is the "any" type in Go 1.18?

In Visual Studio Code, the auto-complete tool (which I presume is gopls?) gives the following template: m.Range(func(key, value any) bool { }) where m is

Modifying a gif image in go...not setting transparency correctly

I have some code which performs the following logical operations: Read in and decode a gif image to a *GIF using gif.DecodeAll Modify some pixels in each frame

Gorm and Gin error 500 only when Updating

I've a pretty simple CRUD for a Task list, and so far I'm able to Create, List all, List by ID and Delete records, bu when I try to update, it gives me the foll

Cross Domain Cookie Golang ReactJs

In Go, I am setting the cookie for frontend: http.SetCookie(w, &http.Cookie{ Name: "jwt-token", Value: tokenString,

Issue with Unmarshalling GRPC Response To Generic Type

I am trying to use generics to convert a JSON object into a GRPC response that has an enum, i.e.: type GRPCResponse { str string enu EnumType } type En

Convert func(T) to func(any) [duplicate]

I want to be able to enforce similarity between two fields of a struct but also have several of these structs in a map or slice. Here's a simp

What is the idiomatic Go equivalent of C's ternary operator?

In C/C++ (and many languages of that family), a common idiom to declare and initialize a variable depending on a condition uses the ternary conditional operator

Mixed named and unnamed function parameters

I have this function for authenticating JWT tokens (not middleware), which says: package main import ( "net/http" "log" "fmt" "github.com/dgrijalva/jwt-go" )

Converting Protobuf3 with enum to JSON in Go

How can I convert grpc/protobuf3 message to JSON where the enum is represented as string? For example, the protobuf message: enum Level { WARNING = 0;

insert decimal type to clickhouse database , there have error of unexpected type string

I am a beginner, I don't understand why insert decimal type field "money", it echo "money (Decimal(18, 2)): unexpected type string" func main() { dsn := "

How can I pretty-print JSON using Go?

Does anyone know of a simple way to pretty-print JSON output in Go? The stock http://golang.org/pkg/encoding/json/ package does not seem to include functionali

Testing gin based REST API not getting params while using net/http/httptest

I am developing a REST API based on Gin Go, the endpoint looks something like below: func carsByType(c *gin.Context) { fmt.Println("Go Request in Handler...

Why does append modify passed slice

How could I iterate through the slice and pass somewhere the slice except the current element? Seems append() function modifies the underlying slice as we could

Sqlite concurrent writing performance

I'm writing a website with Golang and Sqlite3, and I expect around 1000 concurrent writings per second for a few minutes each day, so I did the following test (