Skip to content

Instantly share code, notes, and snippets.

@mh-cbon
Created November 5, 2016 12:34
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 mh-cbon/c89601994e16c3b2eff9dccd1915f0be to your computer and use it in GitHub Desktop.
Save mh-cbon/c89601994e16c3b2eff9dccd1915f0be to your computer and use it in GitHub Desktop.
typed slice alias
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