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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:04:32+00:00 2026-06-12T06:04:32+00:00

Can a Go struct inherit a set of values from a type of another

  • 0

Can a Go struct inherit a set of values from a type of another struct?

Something like this.

type Foo struct {
    Val1, Val2, Val3 int
}

var f *Foo = &Foo{123, 234, 354}

type Bar struct {
    // somehow add the f here so that it will be used in "Bar" inheritance
    OtherVal string
}

Which would let me do this.

b := Bar{"test"}
fmt.Println(b.Val2) // 234

If not, what technique could be used to achieve something similar?

  • 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-06-12T06:04:33+00:00Added an answer on June 12, 2026 at 6:04 am

    Here’s how you may embed the Foo struct in the Bar one :

    type Foo struct {
        Val1, Val2, Val3 int
    }
    type Bar struct {
        Foo
        OtherVal string
    }
    func main() {
        f := &Foo{123, 234, 354}
        b := &Bar{*f, "test"}
        fmt.Println(b.Val2) // prints 234
        f.Val2 = 567
        fmt.Println(b.Val2) // still 234
    }
    

    Now suppose you don’t want the values to be copied and that you want b to change if f changes. Then you don’t want embedding but composition with a pointer :

    type Foo struct {
        Val1, Val2, Val3 int
    }
    type Bar struct {
        *Foo
        OtherVal string
    }
    func main() {
        f := &Foo{123, 234, 354}
        b := &Bar{f, "test"}
        fmt.Println(b.Val2) // 234
        f.Val2 = 567
        fmt.Println(b.Val2) // 567
    }
    

    Two different kind of composition, with different abilities.

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

Sidebar

Related Questions

From a method, I can pass a struct which contains an array of integers,
can we assign null value to struct type of variable? struct MyStruct { }
How can I make this second struct work, when the first struct has a
Why can't I do this: struct sName { vector<int> x; }; It takes only
I can do this in c++/g++: struct vec3 { union { struct { float
Can anybody explain me this statement! pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
What values in the inode struct of the linux kernel can I look at
It's not possible to inherit from a C# struct. It's not obvious to me
This question relates very closely to Can a nested C++ class inherit its enclosing
I've read in several places that a C struct can safely be defined multiple

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.