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 8750465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:52:06+00:00 2026-06-13T12:52:06+00:00

There is the type Company, which is a struct, containing a map of Person’s,

  • 0

There is the type Company, which is a struct, containing a map of Person’s, which are also all structs.

type Company struct {
    employees map[int]Person
}

type Person struct {
    [...]
}

After allocating some Person’s to the employees-map, I’m trying to call a pointer method on each of these.

func (company *Company) Populate(names []string) {
    for i := 1; i <= 15; i++ {
        company.employees[i] = Person{names[i - 1], [...]}
        company.employees[i].Initialize()
    }
}

This miserably fails, with the go-Compiler complaining that I can’t call pointer methods on company.employees[i], as well as I can’t take the address of company.employees[i].
However, setting the Initialize-method to a non-pointer method and letting it return the copy of the person, and allocating it to the map again by using

company.employees[i] = company.employees[i].Initialize()

works, which is not that different.

Not having worked with pointers ever this bugs me quite much. Map’s aren’t immutable, and they get modified either way, so calling a pointer method on an entity in a map shouldn’t be a problem – atleast in my head.

If anyone could explain to me what I’m doing wrong here – or correct my thinking – I’d be pleased.

  • 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-13T12:52:07+00:00Added an answer on June 13, 2026 at 12:52 pm

    The problem here is that in order to call a pointer method, the address of employees[i] needs to be taken. According to the Go specification:

    The operand must be addressable, that is, either a variable, pointer indirection, or slice indexing operation; or a field selector of an addressable struct operand; or an array indexing operation of an addressable array. As an exception to the addressability requirement, x may also be a composite literal.

    A map indexing operation is not addressable. This was decided so that map implementations would not need to guarantee that the addresses of values would not change. As more data is added to the map, it may relocate data for efficiency reasons.

    So, how can you fix it? If you have a map[int]*Person, you will not need to take the address of the data in the map since the map value is already an address.

    One last bit of advice, Person.Initialize() is not very idiomatic Go code. If you need to initialize a type, you normally use a NewPerson() function. The NewPerson() function would return an initialized Person struct or pointer.

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

Sidebar

Related Questions

I need just dictionary or associative array string => int . There is type
Say I have a database with multiple entitles like person, company, conference for which
I have a table which contains two type of data, either for Company or
I have the fallowing NHibernate-Objects: Person inherits from Contact Company inherits from Contact There
Is there some type of cap as to how much CPU an applet can
Why in Boolean type there are two fields with the same value? internal const
There is some type Record : type Day = Integer type Description = String
Is there a jQuery type function to solve the problem of walking through nested
Is there a certain type of SQL field that acts as a sequential number
Is there an R type equivalent to the Matlab structure type? I have a

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.