using reflection in Go to get the name of a struct

前端 未结 3 915
刺人心
刺人心 2021-02-02 09:45

I found this question with this great answers:

How to find a type of a object in Golang?

I played around with the answer and tried to get the name of a struct in

3条回答
  •  情歌与酒
    2021-02-02 10:05

    fmt has a cool %T tag as well

    package main
    
    import (
        "fmt"
        "net/http"
    )
    
    type Potato struct {
    }
    
    func main() {
        fmt.Printf("I have a %T, an %T and a %T\n", Potato{}, http.StatusMultipleChoices, &http.Response{})
    }
    

    outputs I have a main.Potato, an int and a *http.Response https://play.golang.org/p/6z7_0BSitm

提交回复
热议问题