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

  • SEARCH
  • Home
  • 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 8002339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:19:55+00:00 2026-06-04T16:19:55+00:00

Given a typedef of: type ID int Is it possible to convert a map[ID]int

  • 0

Given a typedef of: “type ID int”

Is it possible to convert a map[ID]int to a map[int]int?

I’ve tried:

map[int]int(m)

as well as

m.(map[int]int)

and neither seems to work:
http://play.golang.org/p/oPzkUcgyaR

Any advice?


Edit more details, since a couple people asked.

What I’m trying to do is score a league.

There are a bunch of “Teams”, and each has a number of stats. For each stat, you get 10 points for being highest scoring, 9 for 2nd etc.

I modeled this as:

// Each team's statistics:
type StatLine map[StatID]float64

// The whole league:
map[TeamID]StatLine

// And I want to score that whole league with:
func score(s map[TeamID]StatLine) map[TeamID]int

// Only, is't also handy to score individual players:
func score(s map[PlayerID]StatLine) map[PlayerID]int

And it would be great not to write score() twice (since it’s the same logic) or copy the whole map over.

It happens that PlayerID and TeamID are ints, hence I was curious if I could just write score(s map[int]int) and do type conversion. However, SteveMcQwark makes it clear why this might be a bad idea.

I think generics would solve this, but I know they’re not immediately forthcoming. Any other ideas?

Thanks!

  • 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-04T16:19:57+00:00Added an answer on June 4, 2026 at 4:19 pm

    If you really want to do generics in Go, you need an interface. This interface will be implemented by two types, one for teams and one for players. The score function will take an object of this interface type as an argument and implement the common scoring logic without knowledge of whether it is working with teams or players. That’s the overview. Here are some details:

    The method set of the interface will be exactly the functions that the score function will need. Lets start with two methods it would seem to need,

    type scoreable interface {
        stats(scID) StatLine  // get statLine for ID
        score(scID, int)      // set score for ID
    }
    

    and a generic scoreable ID type,

    type scID int
    

    The types that can implement this interface are not TeamID and PlayerID, but types that hold maps of them. Further, each of these types will need both maps, the StatLine and score maps. A struct works for this:

    type teamScores struct {
        stats map[TeamID]StatLine
        scores map[TeamID]int
    }
    

    Implementing scoreable then,

    func (s *teamScores) stats(id scID) StatLine {
        return s.stats[TeamID(id)]
    }
    
    func (s *teamScores) score(id scID, sc int) {
        s.scores[TeamID(id)] = sc
    }
    

    Wait, you might say. That type conversion of scID to TeamID. Is that safe? Had we might as well just go with the low-enginering approach of not even having TeamID? Well, it’s safe as long as these methods are used sensibly. The struct teamScores associates a map of TeamIDs with another map of TeamIDs. Our soon-to-be-written generic function score takes this struct as an argument and thus is given the correct association. It cannot possibly mix up TeamIDs and PlayerIDs. That’s valuable and in a large enough program can justify this technique.

    Do the same for PlayerID, define a similar struct and add the two methods.

    Write the score function once. Start like this:

    func score(s scoreable) {
        for
    

    Oops, we need some way to iterate. An expedient solution would be to get a list of IDs. Let’s add that method:

    type scoreable interface {
        ids() []scID          // get list of all IDs
        stats(scID) StatLine  // get statLine for ID
        score(scID, int)      // set score for ID
    }
    

    and an implementation for teamScores:

    func (s *teamScores) ids() (a []scID) {
        for tid := range s.stats {
            a = append(a, scID(tid))
        }
        return
    }
    

    Now where were we?

    func score(s scoreable) {
        // lets say we need some intermediate value
        sum := make(map[scID]float64)
        // for each id for which we have a statLine,
        for _, id := range s.ids() { // note method call
            // get the statLine
            stats := s.stats() // method call
            // compute intermediate value
            sum[id] = 0.
            for _, statValue := range stats {
                sum[id] += statValue
            }
        }
        // now compute the final scores
        for id, s := range sum {
            score := int(s) // stub computation
            s.score(id, score) // method call
        }
    }
    

    Notice the function takes the interface type scoreable, not pointer to interface like *scoreable. An interface can hold the pointer type *teamScores. No additional pointer to the interface is appropriate.

    Finally, to call the generic function, we need an object of type teamScores. You probably already have the league stats, but might not have created the score map yet. You can do all this at once like this:

    ts := &teamScores{
        stats:  ...your existing map[TeamID]Statline,
        scores: make(map[TeamID]int),
    }
    

    call:

    score(ts)
    

    And the team scores will be in ts.scores.

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

Sidebar

Related Questions

Given a function prototype, and a type definition: int my_function(unsigned short x); typedef unsigned
Given an integer typedef: typedef unsigned int TYPE; or typedef unsigned long TYPE; I
Given: typedef type-declaration synonym; I can see how: typedef long unsigned int size_t; declares
Given two structure in c: typedef struct _X_ { int virtual_a; int virtual_b; void
I have a struct: typedef struct _n { int type; union { char *s;
Given a template class as such: template <typename TYPE> class SomeClass { public: typedef
I tried the following code #include <stdio.h> int main(void) { typedef static int sint;
Given the following piece of code: template<typename T> class MyContainer { typedef T value_type;
Given: unsigned int a, b, c, d; I want: d = a * b
Given this XML: <?xml version=1.0 encoding=utf-8?> <content dataType=XML> <item type=Promotion name=Sample Promotion expires=04/01/2009> <![CDATA[

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.