Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Golang inheritance example
package encoding
import (
"github.com/stretchr/testify/assert"
"testing"
)
type genericEntity struct {
ID int64
Name string
Texture []byte
}
type tileEntity struct {
genericEntity
}
func NewTileEntity(id int64, name string, texture []byte) *tileEntity {
return &tileEntity{
genericEntity: genericEntity{
ID: id,
Name: name,
Texture: texture,
},
}
}
func TestInheritanceOfNonPointers(t *testing.T) {
tileEntity := tileEntity{genericEntity{
ID: 23,
Name: "this is a cool tile",
Texture: []byte{0x64, 0x45, 0x11},
}}
assert.Equal(t, 23, tileEntity.ID)
assert.Equal(t, "this is a cool tile", tileEntity.Name)
assert.Equal(t, []byte{0x64, 0x45, 0x11}, tileEntity.Texture)
}
func TestInheritance(t *testing.T) {
tileEntity := NewTileEntity(
23,
"this is a cool tile",
[]byte{0x64, 0x45, 0x11},
)
assert.Equal(t, 23, tileEntity.ID)
assert.Equal(t, "this is a cool tile", tileEntity.Name)
assert.Equal(t, []byte{0x64, 0x45, 0x11}, tileEntity.Texture)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment