Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8876481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:13:27+00:00 2026-06-14T19:13:27+00:00

Let’s say I define the following interface in a Go library: type Task interface

  • 0

Let’s say I define the following interface in a Go library:

type Task interface {
    Do() error
}

func Register(task Task) { ... }

func GetId(task Task) int64 { ... }

In Register(), the library associates a unique int64 with each task instance. GetId() must return the identifier for the given task.

My initial idea was to store the association as a map[Task]int64. This seems to work fine, but I was told that it would break if an object implementing Task was not equality-comparable (for example, a struct containing a map). I still need to check if this is true.

I was going to try and use a slice of struct { task Task; id int64 } instead and just iterate over it, but that would still require equality comparable Task instances. And AFAIU there is no identity comparison in Go.

How can I have a robust mapping from Task instances to their ID?

EDIT: Both solutions proposed so far work, but they have the disadvantage that every Task implementation has to include some repetitive code to handle the IDs. I could provide that code in a TaskBase struct that could be embedded, but ideally I would prefer a solution that doesn’t require implementations to even know about the IDs (they are internal to the library and have no meaning outside of it).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:13:30+00:00Added an answer on June 14, 2026 at 7:13 pm

    A more complete example: http://play.golang.org/p/1RzDiw7F9t

    package main
    
    import (
        "fmt"
        "math/rand"
    )
    
    type Task interface {
        Do() error
        ID() int64
    }
    
    type XTask struct {
        id int64
        // other stuff
    }
    
    func NewXTask( /*task parameters...*/) *XTask {
        t := &XTask{ /*initialize members*/}
        t.id = Register(t)
        // possibly more initialization...
        return t
    }
    
    func (t *XTask) Do() error  { return nil }  // stub
    func (t *XTask) ID() int64  { return t.id }
    
    var taskRegistry = map[int64]Task{}
    
    func Register(t Task) int64 {
        var id int64
        for {
            id = rand.Int63()
            if _, exists := taskRegistry[id]; !exists {
                break
            }
        }
        taskRegistry[id] = t
        return id
    }
    
    func main() {
        t1 := NewXTask()
        t2 := NewXTask()
        fmt.Printf("%x\n", t1.ID())
        fmt.Printf("%x\n", t2.ID())
    }
    

    I used an ID method as Daniel suggested, and I turned the map backwards from the way you had it. This is because the Task objects know their own ID, so a map from Task to ID is not needed. A map from ID to task however, is useful for guaranteeing uniqueness. It might come in handy some other time if you find yourself with only an ID and you need the corresponding Task object.

    Also note this example is not goroutine-safe. If you need that you will have to add synchronization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let say I have the following desire, to simplify the IConvertible's to allow me
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have the following classes : public class MyProductCode { private String
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have a domain object with the following field: private Map<StatType, Double>
Let's say I have this interface: // .h @interface DataObject : NSObject { NSString*
Let's say I have the following two lists of tuples myList = [(1, 7),
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.