C# nameof equivalent

635 views
Skip to first unread message

saurabh singh

unread,
Mar 19, 2019, 8:19:18 AM3/19/19
to golang-nuts
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.


Saurabh Singh
B.Tech (Computer Science)
MNNIT 

Ian Lance Taylor

unread,
Mar 19, 2019, 8:53:45 AM3/19/19
to saurabh singh, golang-nuts
On Tue, Mar 19, 2019 at 5:19 AM saurabh singh <saur...@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

Carlos Alberto Costa Beppler

unread,
Mar 19, 2019, 9:00:56 AM3/19/19
to golang-nuts
C# nameof operator is evaluated at compile time.

Go does not have this operator, but it would be an nice addition.

Robert Engels

unread,
Mar 19, 2019, 9:04:16 AM3/19/19
to Ian Lance Taylor, saurabh singh, golang-nuts
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.
> --
> 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...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

saurabh singh

unread,
Mar 19, 2019, 9:18:34 AM3/19/19
to Robert Engels, Ian Lance Taylor, golang-nuts
Yes. I am looking for a compile time operator.
Does the community   think it makes sense to start a new formal  proposal? 

Ian Lance Taylor

unread,
Mar 19, 2019, 10:10:54 AM3/19/19
to saurabh singh, Robert Engels, golang-nuts
On Tue, Mar 19, 2019 at 6:18 AM saurabh singh <saur...@gmail.com> wrote:
>
> Yes. I am looking for a compile time operator.
> Does the community think it makes sense to start a new formal proposal?

Sorry for misunderstanding that this is a purely compile time operation.

I don't personally see why this is worth adding an additional language
feature that all Go programmers will have to learn, but if you do
think it is worth it then a formal proposal is the right way to go.

Ian
Reply all
Reply to author
Forward
0 new messages