I think nameof is a compile time operation (constant generation) not runtime.
I’ve never used it but the ops request seems reasonable. Go 2.0 proposal I guess. > On Mar 19, 2019, at 7:53 AM, Ian Lance Taylor <i...@golang.org> wrote: > >> On Tue, Mar 19, 2019 at 5:19 AM saurabh singh <saurab...@gmail.com> wrote: >> >> Is there an alternative for nameof in golang. >> I don't want reflection magic. >> Use case is to avoid log messages, validation etc to go out of sync with >> code changes: >> >> func foo(correctParamName string) (interface{}, error){ >> if len(correctParamName) == 0{ >> return nil, errors.New("crypticParamNameFixedAfterCodeReview is >> empty") >> } >> } >> >> Much better with >> return nil, errors.New(nameof(crypticParamNameFixedAfterCodeReview) + " is >> empty") // compile error unless this is also changed to match the param name. >> >> Something as simple as nameof else it would be an overkill for the use case. > > Sorry, Go does not provide any way to get the name of a variable at run time. > > Ian > > -- > 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. -- 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.