Skip to content

Latest commit

 

History

History
29 lines (23 loc) · 362 Bytes

name.md

File metadata and controls

29 lines (23 loc) · 362 Bytes

Get struct/interface name

package main

import (
	"fmt"
	"reflect"
)

func main() {
	p := new(Person)
	fmt.Println(p.Name())
}

type Person struct{}

func (p *Person) Name() string {
	return className(p)
}

func className(unknown interface{}) string {
	t := reflect.TypeOf(unknown)
	if t.Kind() == reflect.Ptr {
		t = t.Elem()
	}
	return t.Name()
}