Created
September 22, 2019 13:38
-
-
Save artheus/00e37972b9be53ed9fb71bbb81db001f to your computer and use it in GitHub Desktop.
Golang inheritance example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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