Best way to organize a Go interface
Posted
by Metropolis
on Stack Overflow
See other posts from Stack Overflow
or by Metropolis
Published on 2010-05-21T22:38:44Z
Indexed on
2010/05/21
22:40 UTC
Read the original article
Hit count: 223
go
Hey Everyone,
Its been a long time since I have programmed in C++, and if I remember correctly the best way to organize classes was to create your class in the .h file, and then your implementation in your .cpp file.
Well I am trying to learn Go now and I was reading over the Go for C++ Programmers article when I came upon interfaces. The article explains that interfaces in Go essentially take the place of classes, and shows how to set them up pretty well.
What I am trying to figure out though is how should I organize an interface into files? For instance, should the interface be in one file while the implementation is in another?
myInterface.go
type myInterface interface {
get() int
set(i int)
}
myImplementation.go
type myType struct { i int }
func (p *myType) set(i int) { p.i = i }
func (p *myType) get() int { return p.i }
My code here may be wrong since I do not completely know what I am doing yet (and if I am wrong please correct me), but would this be the best way to set this up? Im having a very hard time trying to wrap my head around how to organize code in Go so any help is appreciated!
Metropolis
© Stack Overflow or respective owner