How i can use go interface in golang as java

Hi team,
I am trying to read data from database from golang.I have two different structs and my function returns these two different structs for same code.
How can i made it generic.
type EventConfigDbDocs struct {
Docs []EventConfiguration json:"docs"
}

type EventDataDbDocs struct {
Docs []Event json:"docs"
}

func getEventConfig(selector string) []entities.EventConfiguration {

//Get the results from find.
eventResult := EventConfigDbDocs{}

return eventResult.Docs
}

Pls help me out here.

Thanks

Kindly help

I don’t yet understand what you are trying to do. Can you show the function that returns either of these types? I see getEventConfig in your original post, but that only returns []entities.EventConfiguration. Are you saying that getEventConfig can also return []entities.Events? If so, then I would like to see how the code knows what type to return.

This i am trying to achive for two different return type in same function.

OK, I think you want 3 functions:

  1. loadQueryResults (or if you have a better name, of course go with that)
  2. getEventData
  3. getEventConfig

loadQueryResults should probably look exactly like your current getEventConfig but the eventResults passed in to Find on line 14 should become a parameter to the function:

func loadQueryResults(selector string, eventResult interface{}) {
    // ...
    // Note: Remove &
   err = db.Find(eventResult, &params)
    // ...
}

And then getEventData becomes:

finc getEventData(selector string) []entities.Event {
    eventResult := struct {
        Docs []entities.Event
    }{}
    loadQueryResults(selector, &eventResult)
    return eventResult.Docs
}

And then getEventConfig becomes:

finc getEventConfig(selector string) []entities.EventConfiguration {
    eventResult := struct {
        Docs []entities.EventConfiguration
    }{}
    loadQueryResults(selector, &eventResult)
    return eventResult.Docs
}

Thanks a lot!!

type Dbdocs interface {
GetDbDocs() interface{}
}

type EventConfigDbDocs struct {
Docs []EventConfiguration json:"docs"
}

type EventDataDbDocs struct {
Docs []Event json:"docs"
}

func (econfigdocs EventConfiguration) GetDbDocs() interface{} {
return EventConfigDbDocs{}

}

func (edatadocs EventDataDbDocs) GetDbDocs() interface{} {
return EventDataDbDocs{}

Is it possible in golang?Create one interface with multiple return valve.If yes then how we can acheive it.

Maybe this is what you want:

package main

import (
	"encoding/json"
	"fmt"
)

type EventConfiguration struct{}
type Event struct{}

type DbDocs[TDbDoc any] struct {
	Docs []TDbDoc `json:"docs"`
}

func loadFromCouch[TDbDoc any](eventResult *DbDocs[TDbDoc], selector string) []TDbDoc {
	// I'm not sure if/where `selector` gets used.
	db := connectcouchdb()
	// On a side note, you don't need to create JSON just to
	// unmarshal it:
	selectorObj := map[string]interface{}{
		"_rev": map[string]interface{}{
			"$eq": refId,	// not sure where `refId` comes from
		},
	}
	params := &couchdb.FindQueryParams{Selector: &selectorObj}
	if err := db.Find(eventResult, params); err != nil {
		fmt.Printf("ERROR: in find: %s", err)
	}
	if len(eventResult.Docs) != 0 {
		fmt.Printf("Find Results Length: %v\n", len(eventResult.Docs))
		fmt.Printf("Find docs: %v\n", eventResult.Docs)
	} else {
		fmt.Printf("Results: %v\n", len(eventResult.Docs))
	}

	return eventResult.Docs
}

func main() {
	cfg := DbDocs[EventConfiguration]{}
	cfgDocs := loadFromCouch(&cfg, "")
	data := DbDocs[Event]{}
	dataDocs := loadFromCouch(&data, "")
	_, _ = cfgDocs, dataDocs
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.