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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:33:57+00:00 2026-05-15T08:33:57+00:00

To define a map from int to struct vertex, should I define map[int]vertex or

  • 0

To define a map from int to struct vertex, should I define map[int]vertex or map[int]*vertex? Which one is preferred?

I extended Chickencha’s code:

package main

type vertex struct {
    x, y int 
}

func main() {
    a := make(map[int]vertex)
    b := make(map[int]*vertex)

    v := &vertex{0, 0}
    a[0] = *v
    b[0] = v 

    v.x, v.y = 4, 4
    println(a[0].x, a[0].y, b[0].x, b[0].y)

    //a[0].x = 3 // cannot assign to (a[0]).x
    //a[0].y = 3 // cannot assign to (a[0]).y
    b[0].x = 3 
    b[0].y = 3 
    println(a[0].x, a[0].y, b[0].x, b[0].y)

    u1 := a[0]
    u1.x = 2 
    u1.y = 2 
    u2 := b[0]
    u2.x = 2 
    u2.y = 2 
    println(a[0].x, a[0].y, b[0].x, b[0].y)
}

The output:

0 0 4 4
0 0 3 3
0 0 2 2

From the output, my understanding is, if I want to change the struct member in place, I must use pointer to the struct.
But I’m still not sure the underlying reasons. Especially, why I cannot assign to a[0].x?

  • 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-05-15T08:33:57+00:00Added an answer on May 15, 2026 at 8:33 am

    The main difference is that map[int]vertex stores vertex values and map[int]*vertex stores vertex references (pointers). The output of the following program should help illustrate:

    package main
    
    type vertex struct {
        x, y int
    }
    
    func main() {
        a := make(map[int]vertex)
        b := make(map[int]*vertex)
    
        v := &vertex{0, 0}
        a[0] = *v
        b[0] = v
    
        v.x, v.y = 4, 4
        println(a[0].x, a[0].y, b[0].x, b[0].y)
    }
    

    Output:

    0 0 4 4
    

    The vertex stored in b is modified by the v.x, v.y = 4, 4 line, while the vertex stored in a is not.

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

Sidebar

Related Questions

I define a template function which loads a map from a CSV file: template
I've got this snippet of code: (define-key lisp-interaction-mode-map (kbd C-c C-e) (lambda () (let
With EF CTP5 Code-First I am trying to map a class model which contains
I want to define something like Map<int, char[5] > myMap; The above declaration is
On REPL, if I define (def fits (map vector (take 10 (iterate inc 0))))
If you had two snippets: (global-set-key \C-d delete-char) and (define-key global-map \C-d delete-char) Is
I want to define a generic function for printing contents of std::map like types.
When we define routes in routes.rb using the name like map.some_link .We can use
I am trying to define a mapping for Ctrl + . as follows: map
I've got a snippet I want to bind to a key: (define-key (lisp-interaction-mode-map) (kdb

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.