Skip to content

Instantly share code, notes, and snippets.

@artheus
Created September 22, 2019 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artheus/00e37972b9be53ed9fb71bbb81db001f to your computer and use it in GitHub Desktop.
Save artheus/00e37972b9be53ed9fb71bbb81db001f to your computer and use it in GitHub Desktop.
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