Skip to content

Instantly share code, notes, and snippets.

@mortdeus
Last active August 29, 2015 14:19
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 mortdeus/847036a9dc2fe544e6a8 to your computer and use it in GitHub Desktop.
Save mortdeus/847036a9dc2fe544e6a8 to your computer and use it in GitHub Desktop.
go interface hackery
package main
import "fmt"
func main() {
s1, asc1 := FindAsc(1)
s2, asc2 := FindAsc(2)
s3, asc3 := FindAsc(3)
s4, asc4 := FindAsc(4)
fmt.Println(fmt.Sprintf(s1, asc1, *((*asc1).(*Asc))))
fmt.Println(fmt.Sprintf(s2, (*asc2).Adsr(2), *((*asc2).(*Asc))))
fmt.Print(fmt.Sprintf(s3, asc3, *((*asc3).(*Asc))))
fmt.Println(fmt.Sprintf(string(db.Find((*asc3).Adsr(3))), asc3, *(*asc3).(*Asc)))
fmt.Print(fmt.Sprintf(s4, asc4, *((*asc4).(*Asc))))
fmt.Println(fmt.Sprintf(string(db.Find((*asc4).Adsr(2))), asc4, *(*asc4).(*Asc)))
}
type DB map[int]StoredAsc
var db = &DB{1: StoredAsc("a"), 2: StoredAsc("b"), 3: StoredAsc("c"), 4: StoredAsc("d")}
func (d *DB) Find(as *AdSRer) StoredAsc {
if v, ok := (*d)[int(*(*as).(*Asc))]; ok {
return StoredAsc(fmt.Sprint("[", as, "] -> ", *((*as).(*Asc)), " :-: ", v, " [%v] -> %v\n"))
}
panic(fmt.Errorf("db.Find(%v) failed.", int(*(*as).(*Asc))))
}
type AdSRer interface {
Adsr(id int) *AdSRer
}
type Asc int
type StoredAsc string
func (asc *Asc) Adsr(id int) *AdSRer {
*asc = Asc(id)
a := AdSRer(asc)
return &a
}
func FindAsc(id int) (val string, asc *AdSRer) {
asc = AdSRer(new(Asc)).Adsr(id)
val = string(db.Find(asc))
return
}
@mortdeus
Copy link
Author

output:

[0x1040a120] -> 1 :-: a [0x1040a120] -> 1

[0x1040a158] -> 2 :-: b [0x1040a1f0] -> 2

[0x1040a188] -> 3 :-: c [0x1040a188] -> 3
[0x1040a208] -> 3 :-: c [0x1040a188] -> 3

[0x1040a1b8] -> 4 :-: d [0x1040a1b8] -> 4
[0x1040a248] -> 2 :-: b [0x1040a1b8] -> 2

Program exited.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment