These questions are not dumb. On the contrary, "by reference" vs "by values" is a fundamental question in most languages, and the source of many headaches when used incorrectly so it's worth taking some time to ask and get it right.
Yes you may change the values of an existing map, inside any function or method. Go doesn't provide idiomatic ways to have "immutable-like" or "view-like" builtin containers, except string which are always immutable. When you have an array, or a slice, or a map, you can always alter its content. Having said this, if you don't want to caller to modify the data, you may : - clone the data (make a copy of the map, you have to explicitly loop for that) and give the clone to the caller. This is called "defensive copying". - or create custom struct type, containing unexported map, with exported read-only accessors e.g. func (c *MyCustomContainer) Get(key string) Value Cheers On Friday, June 24, 2016 at 3:29:25 PM UTC+2, Tong Sun wrote: > > *(Thanks a lot for all your help Val!)* > > One last question, which I wasn't able to find myself from > https://blog.golang.org/go-maps-in-action > > Since a map is a pointer, for any function that take map as a parameter, > or function like, > > func (s set) Something(xxx) > > Does it means the map values can be changed inside the function? > > If yes, what's the proper way to declare it so as to make sure the values > cannot be changed inside the function? > > Thanks, and sorry if the question seems too dumb. > > > ---------- Forwarded message ---------- > From: Valentin Deleplace @gmail.com > Date: Fri, Jun 24, 2016 at 2:11 AM > Subject: Re: [go-nuts] How to init a global map > To: Tong Sun @gmail.com > > No penalty, passing a map around never copies the values. > But a "pointer to a pointer" receiver is not strictly the same as a > pointer receiver. > Le 24 juin 2016 02:19, "Tong Sun" a écrit : > >> >> On Thu, Jun 23, 2016 at 6:54 PM, Val wrote: >> >> Even in OO style (e.g. java), you would not be able to write >>> Set s = null; >>> s.init( list(1,2,3) ); >>> >>> This is (more or less) the same problem with value receiver... the >>> "constructor-like" idiom in go is a function NewSet, not a method : >> >> https://play.golang.org/p/_n56yMhlRt >> >> >> * Now* I understand the reason behind such idiom in go. Yep, everything >> makes sense now. >> >> Thanks a lot for the clear explanation. >> That really helps. >> >> Hmm... wait, >> >> map is a reference type >> >> >> Does that means that, this function >> >> func (s set) Has(a string) bool { _, ok := s[a]; return ok } >> >> is exactly the same as the following? >> >> func (s *set) Has(a string) bool { _, ok := (*s)[a]; return ok } >> >> I.e., even the set is super super big, there is no pass-by-value penalty >> in the first version? >> >> Thx >> >> > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.