Created
January 14, 2014 06:29
-
-
Save theophoric/8414003 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
An interface is a category of type that defines a set of methods. An interface is valid as long as it or the value it holds ( e.g. a struct ) implements those methods | |
*/ | |
package main | |
import ( | |
"fmt" | |
"reflect" | |
) | |
type FrameHeader struct { | |
Type uint8 | |
LongAddr uint64 | |
ShortAddr uint16 | |
Length uint16 | |
} | |
type Frame struct { | |
Header FrameHeader | |
Payload []byte | |
} | |
type Carrier interface { | |
SetHeader() | |
SetPayload() | |
GetHeaderStruct() interface{} | |
} | |
func (f Frame) GetHeader() interface{} { | |
return &FrameHeader{} | |
} | |
func (f Frame) SetHeader() { | |
} | |
func (f Frame) GetPayloadStruct() { return } | |
func (f Frame) SetPayloadStruct() { return } | |
func (f Frame) EncodePayload() { return } | |
func (f Frame) DecodePayload() { return } | |
func main() { | |
f := getFrameStruct() | |
v := reflect.ValueOf(f) | |
e := v.Elem() | |
// Read into new frame type struct | |
// reflect frame type struct | |
if l := e.FieldByName("Length"); l.IsValid() { | |
if l := e.FieldByName("Header"); l.IsValid() { | |
b := make([]byte, header.Length) | |
fmt.Println(b) | |
fmt.Println(p) | |
fmt.Println(p.CanSet()) | |
p.SetBytes(b) | |
v2 := reflect.ValueOf(p) | |
fmt.Println(v2.Interface()) | |
fmt.Println(e) | |
fmt.Println(f) | |
} | |
// if field := v.FieldByName("Header"); field.IsValid() { | |
// byteBuf := make([]byte, 24) | |
// fmt.Print(byteBuf) | |
// field.SetBytes(byteBuf) | |
// } | |
// f := getFrameStruct() | |
// if carrier, ok := interface{}(f).(Carrier); ok { | |
// // carrier now has access to all methods defined in type Carrier interface{} | |
// headerStruct := carrier.GetHeaderStruct() | |
// t1 := reflect.TypeOf(carrier) | |
// t2 := reflect.TypeOf(headerStruct) | |
// fmt.Print(t1, t2) | |
// } else { | |
// fmt.Println("nope") | |
// } | |
// if reflect.TypeOf(s) == reflect.TypeOf(Frame{}) { | |
// fmt.Print("yes") | |
// } | |
// switch v := s.(type) { | |
// case Carrier: | |
// fmt.Println(v) | |
// default: | |
// fmt.Print("default") | |
// } | |
// v := reflect.ValueOf(s) | |
// fmt.Printf("%s", v.Kind()) | |
} | |
func testCarrier(c *Carrier) { | |
return | |
} | |
func getFrameStruct() Carrier { | |
return &Frame{} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment