Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6064765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:18:57+00:00 2026-05-23T09:18:57+00:00

type t1 struct { i int; s string } var v1 reflect.Type = /*

  • 0
type t1 struct { i int; s string }
var v1 reflect.Type = /* how to set to t1's reflect.Type? */
  1. is it possible to get the reflect.Type of t1 without having to instantiate it?

  2. is it possible to get the reflect.Type of t1 from having its name “t1” as a string?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:18:58+00:00Added an answer on May 23, 2026 at 9:18 am

    On 1, yes, kinda:

    var v1 reflect.Type = reflect.TypeOf((*t1)(nil)).Elem()
    fmt.Println(v1)  // prints "main.t1"
    

    No instantiation needed. However, Go doesn’t have type literals, which is I think what you’re asking for. To get the runtime value of a type, you need to have a value of some sort. If you don’t want to or can’t create the value at runtime, you can pull it from a typed nil. If you don’t like the idea of looking this up every time, you can store this runtime type in a variable.

    On 2, no, not really. This would require the Go runtime to maintain a map of all types in the current binary, which has a number of problems. You could create a type registry package, and register all types you may want to retrieve by string, but that’s always going to be incomplete, and if you know what type you want, you can always just use TypeOf. The situation is made a bit more complicated by the fact that you can have anonymous types, and the name like “t1” isn’t certain to be unique, as another package may have a type of the same name. It is possible for the Go runtime to provide a function that gives a type from a string name, but I doubt that will happen.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

struct A { std::string get_string(); }; struct B { int value; }; typedef boost::variant<A,B>
I have a struct (.NET 3.5): struct ColumnHeadings { public string Name ; public
I have this custom type: public struct PasswordString { private string value; public PasswordString(string
In .NET, a value type (C# struct ) can't have a constructor with no
I have a type which I consider use it as struct. It represents single
Type.GetType(System.String) Is there a lookup for the aliases available somewhere? Type.GetType(string) returns null .
Given a handle of type HWND is it possible to confirm that the handle
I've an external DLL written in C++. The piece below declares a struct type
I have written code to TryParse enum either by value or by its name
I am struggling with the code from few days can anyone help std::string str=uri_req1.substr(found+1);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.