[Solved] Generic Programming in Go. Avoiding hard coded type assertion

Finally i find a way to do that. Follow the Go Playground and code snippet below: GO Playground: Avoiding Hard Coded Type Assertion //new approach SetAttribute, without any hard coded type assertion, just based on objectType parameter func SetAttribute(myUnknownTypeValue *interface{}, attributeName string, attValue interface{}, objectType reflect.Type) { // create value for old val oldValue := … Read more

[Solved] Append int value to string

You can use strconv from the strconv package package main import ( “fmt” “strconv” ) func main() { a := 4 b := 3 c := “1” fmt.Println(c + strconv.Itoa(a) + strconv.Itoa(b)) } Or you can use Sprintf from the fmt package: package main import ( “fmt” ) func main() { a := 4 b … Read more

[Solved] How to include external file in Go?

You import packages by import path. For package Helper, located in $GOPATH/src/Helper/, use: import “Helper” While they can work in some cases, relative paths aren’t supported by the go toolchain, and are discouraged. 2 solved How to include external file in Go?

[Solved] Go JSON Naming strategy about extends

Please, add your convert code here. The code below works fine. type BaseModel struct { Id string `json:”id”` CreatedTime time.Time `json:”createdTime”` UpdatedTime time.Time `json:”updatedTime”` Deleted bool `json:”deleted”` } type Category struct { BaseModel Parent string `json:”parent”` Name string `json:”name”` IconClass string `json:”iconClass”` Mark string `json:”mark”` } func main() { data, err := json.Marshal(Category{}) if err … Read more

[Solved] Unable to run Golang application on Docker

Change run.sh to replace port 8080 to 8082 #!/bin/bash echo “Listening on http://localhost:8082” docker run -p 8082:80 codetest I have changes port to 8082 if the port is already in use change that port again to some other port based on your available port. If you are on Windows netsh interface portproxy add v4tov4 listenport=8082 … Read more

[Solved] Too many arguments to return

You’ve declared the function GetDBConnection() to return no arguments. func GetDBConnection() { You have to tell Go the type of the argument you intend to return: func GetDBConnection() *sqlx.DB { As for determining the type, I just went to look at the source code. You could also look at the documentation on godoc.org, which is … Read more

[Solved] How to make an api call faster in Golang?

Consider a worker pool pattern like this: https://go.dev/play/p/p6SErj3L6Yc In this example application, I’ve taken out the API call and just list the file names. That makes it work on the playground. A fixed number of worker goroutines are started. We’ll use a channel to distribute their work and we’ll close the channel to communicate the … Read more

[Solved] Getting an array of arrays from a POST operation into a GO calculation through reqBody and json unmarshal [closed]

fmt.Println, won’t be showing “,” between each element of array if you want to print it that way you need to format and print it var datas [][]string json.Unmarshal(reqBody, &datas) fmt.Println(“this is after the unmarshalling”) array := []string{} for _, data := range datas { array = append(array, fmt.Sprintf(“[%s]”, strings.Join(data, “,”))) } output := fmt.Sprintf(“[%s]”, … Read more

[Solved] How to develop golang modules efficiently [closed]

While you’re developing, I’d recommend just using replace directives in your go.mod to make any changes in dependencies instantly visible (regardless of version) to client code. E.g. if you have package “client” using package “auth”: $SOMEDIR/client/go.mod would replace dependency on client with $SOMEDIR/auth, and now you can just develop the two alongside each other in … Read more

[Solved] Combining string and []byte for payload

Here’s an example using multipart request. I modified this from a piece of code I have that deals with JSON docs, so there may be some mistakes in it, but it should give you the idea: body := bytes.Buffer{} writer := multipart.NewWriter(&body) hdr := textproto.MIMEHeader{} hdr.Set(“Content-Type”, “text/plain”) part, _ := writer.CreatePart(hdr) part.Write(data1) hdr = textproto.MIMEHeader{} … Read more

[Solved] Read specific number of bytes which is in acceptable data

Try using this: package main import ( “encoding/binary” “io” ) func ReadPacket(r io.Reader) ([]byte, error) { lenB := make([]byte, 4) if _, err := r.Read(lenB); err != nil { return nil, err } //you can use BigEndian depending on the proto l := binary.LittleEndian.Uint32(lenB) packet := make([]byte, l) _, err := r.Read(packet) return packet, err … Read more