Category "go"

GO storing data returns EOF

I'm new to golang and I'm trying to build rest api, So far GET endpoints are working for me fine, but I'm having difficulties with POST method(creating user): T

How can I create a index in Elasticsearch with `go-elasticsearch` library?

I am using this library in go as Elasticsearch client: https://pkg.go.dev/github.com/elastic/go-elasticsearch/esapi#IndicesCreate.WithBody I have a problem on c

Unmarshal to a interface type

I have some code I've been dumped with and am actually stumped - I've worked with RPC and the JSON side of things before but I can't seem to get it to work over

Json dynamic fields to struct issue [duplicate]

Every time I resolve one issue I get into another similar but different. I need to convert into struts below JSON, but some parts are dynamic.

Difference between any/interface{} as constraint vs. type of argument?

As generics have been released in Go 1.18 pretty recently, I've started learning them. I generally get the concept, because I have some Java experience from the

Detect 32 or 64 bits machine (Understanding `32 << (^uint(0) >> 63)` )

It is said that the 32 << (^uint(0) >> 63) expression can be used to detect whether the machine is 32 or 64 bits. How so? UPDATE: The question was c

Cannot unmarshal a post json python request in Go

Here is my python code (client side) : import requests import json import datetime headers = {'Content-type': 'application/json',"Authorization":"Bearer MYRE

Why would return parameters be named?

What benefits arise from naming a function's return parameter(s)? func namedReturn(i int) (ret int) { ret = i i += 2 return } func anonReturn(i in

How to flush Stdin after fmt.Scanf() in Go?

Here's an issue that's bedeviling me at the moment. When getting input from the user, I want to employ a loop to ask the user to retry until they enter valid in

cannot convert data (type interface {}) to type string: need type assertion

I am pretty new to go and I was playing with this notify package. At first I had code that looked like this: func doit(w http.ResponseWriter, r *http.Request)

How to get the pointer of return value from function call?

I just need a pointer to time.Time, so the code below seems invalid: ./c.go:5: cannot take the address of time.Now() I just wonder why? Is there any way t

How to bulk remove objects in minio with golang

I'm trying to bulk remove objects in minio as described here: objectsCh := make(chan minio.ObjectInfo) // Send object names that are needed to be removed to ob

Is there an efficient way to calculate execution time in golang?

I'm looking for the best way to calculate execution time in go. func main() { start := time.Now() time.Sleep(time.Second * 2) //something doing he

Check if a value is in a list

Does Go have something similar to Python's in keyword? I want to check if a value is in a list. For example in Python: x = 'red' if x in ['red', 'green', 'ye

Assign Golang variable to Javascript

Currently I am having an issue related to assign a Golang variable to a Javascript variable. I am using the Golang templates, so, from the backend I sent a JSON

How to overwrite file content in golang

I have a empty file called a.txt, I want to output a value(int) to it in a loop, and overwrite last content in file a.txt. For example, // open a file

Declare a constant array

I have tried: const ascii = "abcdefghijklmnopqrstuvwxyz" const letter_goodness []float32 = { .0817,.0149,.0278,.0425,.1270,.0223,.0202, .0609,.0697,.0015,.0077

Trim string's suffix or extension?

For example, I have a string, consists of "sample.zip". How do I remove the ".zip" extension using strings package or other else?

"package XXX is not in GOROOT" when building a Go project

I have a weird issue that arose when I took a break from this project. Upon starting up Goland, I'm riddled with errors when trying to run my project. The spe

Go StartsWith(str string)

Is there a StartsWith(str1, str2 string) function that can check if str1 is a prefix of str2 in Go language? I want a function similar to the Java's startsWith