Pointer to Struct in Interface, Syntax Error? WTF?

Anyone know why I am getting syntax error in this file? I have done this in other files for Phone, Email, Person, etc and its fine. But for some reason this file will not take a pointer to a struct.

package interfaces

//Go library for decoding generic map values into native Go structures.
//github.com/mitchellh/mapstructure
//Utilities for Go structs
//github.com/fatih/structs
//TODO: Create new Public repo and Combine required source from each of these two
//repos. We need to same for Struct and interface and Convert to/from each as well
//as the methods for getting / working with the field, values list, etc.
/*
func (s *struct) ToMap() Map{interface}
func (Map{interface}) FromMap() *struct

func (s *struct) FillInterface(m Map{Interface})
func (Map{interface}) FillStruct(s *struct)

..Incomplete ...

*/

/******************************************************************************************
 * Base Interface Definition															  *
 ******************************************************************************************/

//BaseStruct is only used for building/testing the base interface and will be removed.
 type BaseStruct struct {
	testInt int
	testBool bool
	testString string
	testByte []byte
	testFloat float64
	testComplex complex128
}

//BaseInterface contains the support methods required for interacting with a structure
type BaseInterface interface {

	//IsStructValid returns true|false if the current struct data is in valid and complete format
	IsStructValid() bool

	//IsStringValid returns true|false if the passed in string value is in valid and complete format
	IsStringValid(s string) bool

	//Get returns the current {model}Struct for the interface
	Get()

	//Set is given all inteface properties and they are assigned to the {model}Struct
	Set()

	//Parse takes a string and parses it to find and assign the individual struct properties of
	//the interfaces parent
	Parse(s string)

	//Format returns a formatted string for the interface
	Format()

	//ToMap Converts {model}Struct to a map[string]interface{}
	ToMap()

	//FromMap converts map[string]interface{} to {model}Struct
	FromMap()

	//ToJSON converts (model}Struct to a JSON and returns it
	ToJSON()

	//FromJSON takes a JSON and converts it to {model}Struct
	FromJSON()
}

//TestBaseInterface is only used for building/testing the base interface and will be removed.
type TestBaseInterface interface {
	*BaseStruct  //<-- Syntax Error: unexpected *, expecting method or interface name? WTF?
	BaseInterface
}

//IsStructValid returns true|false if the current struct data is in valid and complete format
func (p *BaseStruct) IsStructValid() bool {}

//IsStringValid returns true|false if the passed in string value is in valid and complete format
func (p *BaseStruct) IsStringValid(s string) {}

//Get returns the current {model}Struct for the interface
func (p *BaseStruct) Get() {}

//Set is given all inteface properties and they are assigned to the {model}Struct
func (p *BaseStruct) Set() {}

//Parse takes a string and parses it to find and assign the individual struct properties of
//the interfaces parent
func (p *BaseStruct) Parse(s string) {}

//Format returns a formatted string for the interface
func (p *BaseStruct) Format() {}

//ToMap Converts {model}Struct to a map[string]interface{}
func (p *BaseStruct) ToMap() {}

//FromMap converts map[string]interface{} to {model}Struct
func (p *BaseStruct) FromMap() {}

//ToJSON converts (model}Struct to a JSON and returns it
func (p *BaseStruct) ToJSON() {}

//FromJSON takes a JSON and converts it to {model}Struct
func (p *BaseStruct) FromJSON() {}

What am I missing here?

Interfaces are composed from functions and other interfaces. Not from pointers to those. So pointers are not allowed in an interface definition.

OK, So why is this one not giving the same error?

//EmailStruct contains a breakdown of a email
//TODO: Update to use/integrate "net/mail" and Address
type EmailStruct struct {
	Name   string //bob1234
	Domain string //gmail.com
}

//Email contains the methods for interacting with Emails (EmailStruct)
type Email interface {
	*EmailStruct
	intf.BaseInterface
	intf.DGraphInterface
	intf.BadgerInterface
	intf.FileInterface
	intf.JSONInterface
	intf.ViewInterface
}

And what would be the proper way in Golang for me to have a Struct for the “Properties” and standard interfaces for working with those properties?

Is this the proper way?

package interfaces

//Go library for decoding generic map values into native Go structures.
//github.com/mitchellh/mapstructure
//Utilities for Go structs
//github.com/fatih/structs
//TODO: Create new Public repo and Combine required source from each of these two
//repos. We need to same for Struct and interface and Convert to/from each as well
//as the methods for getting / working with the field, values list, etc.
/*
func (s *struct) ToMap() Map{interface}
func (Map{interface}) FromMap() *struct

func (s *struct) FillInterface(m Map{Interface})
func (Map{interface}) FillStruct(s *struct)

..Incomplete ...

*/

/******************************************************************************************
 * Base Interface Definition															  *
 ******************************************************************************************/

//BaseStruct is only used for building/testing the base interface and will be removed.
type BaseStruct struct {
	testInt     int
	testBool    bool
	testString  string
	testByte    []byte
	testFloat   float64
	testComplex complex128
}

//BaseInterface contains the support methods required for interacting with a structure
type BaseInterface interface {

	//IsStructValid returns true|false if the current struct data is in valid and complete format
	IsStructValid() bool

	//IsStringValid returns true|false if the passed in string value is in valid and complete format
	IsStringValid(s string) bool

	//Get returns the current {model}Struct for the interface
	Get()

	//Set is given all inteface properties and they are assigned to the {model}Struct
	Set()

	//Parse takes a string and parses it to find and assign the individual struct properties of
	//the interfaces parent
	Parse(s string)

	//Format returns a formatted string for the interface
	Format()

	//ToMap Converts {model}Struct to a map[string]interface{}
	ToMap()

	//FromMap converts map[string]interface{} to {model}Struct
	FromMap()

	//ToJSON converts (model}Struct to a JSON and returns it
	ToJSON()

	//FromJSON takes a JSON and converts it to {model}Struct
	FromJSON()
}

//TestBaseInterface is only used for building/testing the base interface and will be removed.
type TestBaseInterface struct {
	//*BaseStruct properties for this interface
	*BaseStruct

	//BaseInterface forces implementation of all methods
	BaseInterface
}

//IsStructValid returns true|false if the current struct data is in valid and complete format
func (p *BaseStruct) IsStructValid() bool { return false }

//IsStringValid returns true|false if the passed in string value is in valid and complete format
func (p *BaseStruct) IsStringValid(s string) {}

//Get returns the current {model}Struct for the interface
func (p *BaseStruct) Get() {}

//Set is given all inteface properties and they are assigned to the {model}Struct
func (p *BaseStruct) Set() {}

//Parse takes a string and parses it to find and assign the individual struct properties of
//the interfaces parent
func (p *BaseStruct) Parse(s string) {}

//Format returns a formatted string for the interface
func (p *BaseStruct) Format() {}

//ToMap Converts {model}Struct to a map[string]interface{}
func (p *BaseStruct) ToMap() {}

//FromMap converts map[string]interface{} to {model}Struct
func (p *BaseStruct) FromMap() {}

//ToJSON converts (model}Struct to a JSON and returns it
func (p *BaseStruct) ToJSON() {}

//FromJSON takes a JSON and converts it to {model}Struct
func (p *BaseStruct) FromJSON() {}

Not to have that kind of coupling…

type Aging interface {
  SetAge(int)
  GetAge() int
}

Then the actual implementor has to decide how to store the necessary state.

type foo int

func (f *foo) SetAge(age int) {
  f = age
}

func (f *foo) GetAge() int {
  return int(*f)
}

Also, your second example gives the same syntax error on the playground than the example from your original post…

edit

PS: My example code from above is untested and semi-pseudo code, but it should show the idea.

Its fine I get the idea thanks.
Actually the email one way wrong as well. it was Interface instead of struct like the others. I dont know why it was not showing syntax error but basically BaseTestInterface needs to be a struct that implements a pointer to the struct and whatever interfaces. think I got it now.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.