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 6716509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:44:32+00:00 2026-05-26T08:44:32+00:00

In Go, how do you create the instance of an object from its type

  • 0

In Go, how do you create the instance of an object from its type at run time? I suppose you would also need to get the actual type of the object first too?

I am trying to do lazy instantiation to save memory.

  • 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-26T08:44:33+00:00Added an answer on May 26, 2026 at 8:44 am

    In order to do that you need reflect.

    package main
    
    import (
        "fmt"
        "reflect"
    )
    
    func main() {
        // one way is to have a value of the type you want already
        a := 1
        // reflect.New works kind of like the built-in function new
        // We'll get a reflected pointer to a new int value
        intPtr := reflect.New(reflect.TypeOf(a))
        // Just to prove it
        b := intPtr.Elem().Interface().(int)
        // Prints 0
        fmt.Println(b)
    
        // We can also use reflect.New without having a value of the type
        var nilInt *int
        intType := reflect.TypeOf(nilInt).Elem()
        intPtr2 := reflect.New(intType)
        // Same as above
        c := intPtr2.Elem().Interface().(int)
        // Prints 0 again
        fmt.Println(c)
    }
    

    You can do the same thing with a struct type instead of an int. Or anything else, really. Just be sure to know the distinction between new and make when it comes to map and slice types.

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

Sidebar

Related Questions

I need this roadmap of a Hibernate managed object instance. First, I create an
I need to create an instance of an object and the type of that
I'm trying to create an instance of a generic class using a Type object.
Suppose you create a generic Object variable and assign it to a specific instance.
My ASP.NET application loads an assembly and creates a class instance from an object
Example: I have an class that inherits from UIImageView. An object creates an instance
How can I create an instance of an object using a class reference, and
In python, it is illegal to create new attribute for an object instance like
to create an instance of another class from a class even if the class
I am getting following error: COM object that has been separated from its underlying

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.