Created
February 17, 2017 13:18
-
-
Save niamtokik/89552006d647d0ef767dd8dd56a4f161 to your computer and use it in GitHub Desktop.
way to define triangle and pyramid in go (work on pointers)
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" | |
// import "crypto/sha1" | |
// import "path/filepath" | |
// import "os" | |
type triangle struct { | |
connect [3]*triangle | |
} | |
func (t *triangle) Connect(target *triangle) { | |
if t == target { | |
fmt.Println("can't add myself") | |
return | |
} | |
for i := range t.connect { | |
if t.connect[i] == target { | |
fmt.Println("already connected") | |
return | |
} | |
if t.connect[i] == nil { | |
t.connect[i] = target | |
target.Connect(t) | |
fmt.Println(t) | |
return | |
} | |
} | |
} | |
func (t *triangle) Get() { | |
fmt.Printf("%p :%v\n", t, *t) | |
} | |
func (t *triangle) isConnectedTo(target *triangle) bool { | |
for i := range t.connect { | |
if t.connect[i] == target { | |
return true | |
} | |
} | |
return false | |
} | |
type pyramid struct { | |
facet [4]*triangle | |
} | |
func (p *pyramid) Facet(t *triangle) { | |
for i := range p.facet { | |
if p.facet[i] == nil { | |
p.facet[i] = t | |
return | |
} | |
} | |
} | |
func main() { | |
var p pyramid | |
var t1 triangle | |
var t2 triangle | |
var t3 triangle | |
var t4 triangle | |
t1.Connect(&t2) | |
t1.Connect(&t3) | |
t2.Connect(&t3) | |
t4.Connect(&t1) | |
t4.Connect(&t2) | |
t4.Connect(&t3) | |
t1.Get() | |
t2.Get() | |
t3.Get() | |
t4.Get() | |
p.Facet(&t1) | |
p.Facet(&t2) | |
p.Facet(&t3) | |
p.Facet(&t4) | |
fmt.Println(p) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment