-1

I would like to get the string name of a field like it is possible to do in C#:

if (x == null)
    throw new ArgumentNullException(nameof(x));

In GO I have the following:

package main

type Test struct{
    X int
    Y string
}

func main() {
    fmt.Println(nameof(Test.X))
}

How I can implement the nameof func?

Iain Duncan
  • 3,139
  • 2
  • 17
  • 28
Alexander
  • 833
  • 2
  • 8
  • 17
  • 1
    May we know why you want this. I have the feeling this is an [X Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – RickyA Dec 06 '18 at 11:02
  • For a list of all useful cases on a different language see this link: https://stackoverflow.com/questions/31695900/what-is-the-purpose-of-nameof . I know that is a different language but I am sure there could be some benefits. – Tono Nam Apr 16 '22 at 18:02

2 Answers2

3

How I can implement nameof func?

You cannot.

Fortunately you need not. While writing the code you know the name and can type the string literal as fast as nameof().

(Okay, you technically can, by inspecting the current function an looking up debug symbols but this is complicated and probably non-portable and dependent on the compiler version used. So: Don't even try.)

Volker
  • 40,468
  • 7
  • 81
  • 87
  • Thanks. But unfortunally I need :) I need a dictinary with names of some fields, And if the field name is changed I would like to see the problem on the compile time. – Alexander Dec 06 '18 at 08:24
  • 2
    @Alexander. No. You need to rethink the problem. – Volker Dec 06 '18 at 08:41
  • 2
    @Alexander You really should explain why you need this, so we can suggest an alternative. Whatever the implementation of `nameof()` would be, that will only run at runtime, so you can't get any compile time error out of it. – icza Dec 06 '18 at 08:44
  • @icza Why you need something like this is because of easiness of refactoring. So you don't actually need it, but it makes it easier to refactor and prevent bugs that occur when you forget to rename some hard-coded strings you used for field names. Off course, you can cover this in a test, but discovering problems at compile time is just even better. – Nick N. Aug 11 '20 at 06:18
-1

Lets say you have this struct

type User struct{
    Id string
    Name string
    // etc
}

Then you can have something like this:

query :=  fmt.Sprintf("select * from Users where %s like %s", nameof(User.Name), "foo") 

obviously in go the nameof function does not exists. You could replace that with reflect.TypeOf(User.Name).Name()

That is helpful because if you where to modify your database and replace Name with FirstName and LastName then your code will no longer compile.

Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • does not build: `User.Name undefined (type User has no method Name)` – ZivS Jan 16 '23 at 06:55
  • Read the entire answer. I am saying that the name of that funtion does not exist and was showing an example of how you use it in different languages like on c#. – Tono Nam Jan 16 '23 at 18:07
  • `reflect.TypeOf(User.Name).Name()` is in Go, and does not compile – ZivS Jan 17 '23 at 08:35