On Saturday, August 27, 2016 at 11:34:05 AM UTC+8, T L wrote: > > > > On Tuesday, August 23, 2016 at 1:25:03 AM UTC+8, con...@superhuman.com > wrote: >> >> Hey All, >> >> I've been using a typedef of >> >> type MaybeTimestamp *int64 >> >> so we can JSON encode timestamps correctly, see >> https://medium.com/coding-and-deploying-in-the-cloud/time-stamps-in-golang-abcaf581b72f >> >> for inspriation >> >> I was surprised that the following didn't work (see >> https://play.golang.org/p/1QQylqTLkB): >> >> func NewMaybeTimestamp(t time.Time) MaybeTimestamp { >> return &t.Unix() >> } >> >> // fails with: >> // tmp/sandbox449672725/main.go:14: cannot take the address of >> t.Unix() >> >> I can fix this by introducing a temporary variable: >> >> func NewMaybeTimestamp(t time.Time) MaybeTimestamp { >> temp := t.Unix() >> return &temp >> } >> >> Seeing as this is the obvious solution to this problem, I was pretty >> disappointed that the compiler couldn't insert the temporary for me. >> >> Is this something that should change, or is it just a limitation >> hard-coded by the compatibility guarantee? >> > > Direct values, including const and temp values, are not addressable. But > there is an exception which is described in Ian's answer. > >
The counter rule is: variables, including elements of variables, are always addressable. And there is also an exception for the counter rule: map elements are not addressable. Please adapt it. Almost for every rule in Golang, there are some exceptions. > >> Conrad >> >> -- 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.