Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize inherited object's fields

Tags:

go

I must be missing something. I cannot initialize an object's inherited fields without directly accessing them.

My goal is trying to keep it simple.

package main

type Page struct {
  Title string
}

type Article struct {
  Page
  Id int
}

func main() {

  // this generates a build error: 
  // "invalid field name Title in struct initializer"
  //
  p := &Article{
    Title: "Welcome!",
    Id:    2,
  }

  // this generates a build error: 
  // "invalid field name Page.Title in struct initializer"
  //
  p := &Article{
    Page.Title: "Welcome!",
    Id:         2,
  }

  // this works, but is verbose...  trying to avoid this
  //
  p := &Article{
    Id:    2,
  }
  p.Title = "Welcome!"

  // as well as this, since the above was just a shortcut
  //
  p := &Article{
    Id:    2,
  }
  p.Page.Title = "Welcome!"

}

Thanks in advance.

like image 577
eduncan911 Avatar asked Jun 05 '14 03:06

eduncan911


People also ask

What are the 3 ways of object initialization?

Assigning value to a variable is called initialization of state of an object. Initialization of variable means storing data into an object. 3. We can assign the value of variables in three ways: by using constructor, reference variable, and method.

How do you create an instance of an inherited class?

The derived class inherits the base class member variables and member methods. Therefore, the super class object should be created before the subclass is created. You can give instructions for superclass initialization in the member initialization list. Here you can see object is created for the inherited class.

Which method is used to initialize objects?

A constructor is used to initialize an object when it is created. It is syntactically similar to a method. The difference is that the constructors have the same name as their class and, have no return type. There is no need to invoke constructors explicitly these are automatically invoked at the time of instantiation.

How do you initialize a new object in Java?

Creating an Object In Java, the new keyword is used to create new objects. Declaration − A variable declaration with a variable name with an object type. Instantiation − The 'new' keyword is used to create the object. Initialization − The 'new' keyword is followed by a call to a constructor.

How do I use object initializers with named objects?

The following examples show how to use object initializers with named objects. The compiler processes object initializers by first accessing the parameterless instance constructor and then processing the member initializations.

What happens if you don’t initialize a field?

Fields have a default value so if you don’t initially assign a value to them, they get initialized with that default value. That being said, you must be careful about final instance variables (non-static fields): they must be initialized when an instance is constructed, or code will not compile.

What is serialization inheritance in Java?

Object Serialization with Inheritance in Java. Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to persist the object.

Can a base class inherit from a derived class?

In other words, type Dcan inherit from type C, which inherits from type B, which inherits from the base class type A. Because inheritance is transitive, the members of type Aare available to type D. Not all members of a base class are inherited by derived classes.


1 Answers

In Go, these fields from embedded structs are called promoted fields.

The Go Specification states (my emphasis):

Promoted fields act like ordinary fields of a struct except that they cannot be used as field names in composite literals of the struct.

This is how you can solve it:

p := &Article{
    Page: Page{"Welcome!"},
    Id:   2,
}
like image 70
ANisus Avatar answered Sep 22 '22 20:09

ANisus