Created
November 5, 2016 12:34
-
-
Save mh-cbon/c89601994e16c3b2eff9dccd1915f0be to your computer and use it in GitHub Desktop.
typed slice alias
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 main | |
import("fmt") | |
type MyStruct struct{ | |
x string | |
} | |
type MyTypedSlice []*MyStruct | |
func (f *MyTypedSlice) Add(s *MyStruct) *MyTypedSlice { | |
*f = append(*f, s) | |
return f | |
} | |
func main () { | |
k := &MyTypedSlice{} | |
k.Add(&MyStruct{x: "hello"}) | |
fmt.Printf("%+v\n", *k) | |
fmt.Printf("%+v\n", *(*k)[0]) | |
// prints | |
// [0x1040a120] | |
// {x:hello} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment