Here is a good Throw down of Go's difference's from mainstream languages and an introduction to its shiny new Stuff.
Dove in First
Monday, March 15, 2010
Setup the Vim Editor for Go in Linux
1. Complete Go Installation
2. After completing the installation of the Go code base and building you need to copy the vim syntax file into your vim syntax file directory. There are many other places you could put the go.vim syntax file. This is the way I did it.
3. Now we have to tell vim what kind of files to use the go syntax for. There are many ways to do this, this is what I did.
Now you should be setup to begin writing your Go code in vim with syntax highlighting.
EDIT: This is all irrelevant. The readme inside of $GOROOT/misc/vim explains one of the best ways to do this.
There is also a file in the $GOROOT/misc/bash directory that enables bash tab completion for the go compilers and linkers. Copy this into /etc/bash_completion.d/
2. After completing the installation of the Go code base and building you need to copy the vim syntax file into your vim syntax file directory. There are many other places you could put the go.vim syntax file. This is the way I did it.
sudo cp $GOROOT/misc/vim/go.vim /usr/share/vim/vim72/syntax/
3. Now we have to tell vim what kind of files to use the go syntax for. There are many ways to do this, this is what I did.
mkdir ~/.vim/ftdetect
echo "au BufRead,BufNewFile *.go set filetype=go" > ~/.vim/ftdetect/go.vim
Now you should be setup to begin writing your Go code in vim with syntax highlighting.
EDIT: This is all irrelevant. The readme inside of $GOROOT/misc/vim explains one of the best ways to do this.
There is also a file in the $GOROOT/misc/bash directory that enables bash tab completion for the go compilers and linkers. Copy this into /etc/bash_completion.d/
sudo cp $GOROOT/misc/bash/go /etc/bash_completion.d/
Classic Inheritance in Go Lang
Tar of this Project, extract and run make. Creates binary ChangeMe.app
BasicGoProject.tar.gz
Package and Imports
BasicGoProject.tar.gz
Package and Imports
Interface that our Inheritance chain will implement. Analogous to an abstract Base Class in classic Object Oriented Terms.
package main
import (
"fmt"
)
This is the base class for this chain of inheritance.
// Declare an Interface to a 3d Solid
type Solid interface {
Volume() float
SurfaceArea() float
}
Implement the Solid Interface for RectPrism.
// Contains the Fields for defining a Rectangular Prism's Dimension's
type RectPrism struct {
l, w, h float
}
Here is Our first subclass of RectPrism. It inherits all the Fields and Methods attached to RectPrism by listing RectPrism as an anonymous Field. CardboardBox adds a Field aswell since CardboardBoxes can be Soggy.
// RectPrism implements the Solid Interface
func (this *RectPrism) Volume() float {
return(this.l * this.w * this.h)
}
func (this *RectPrism) SurfaceArea() float {
return(2 * (this.l * this.w) + 2 * (this.l * this.h) + 2 * (this.w * this.h))
}
An Open Topped Cardboard Box has to Reimplement the SurfaceArea Method of the Solid interface.
type CardboardBox struct {
// An anonymous field, all fields of RectPrism are promoted into CardboardBox
RectPrism
isSoggy bool
}
We get to reuse the parent class's implementation of SurfaceArea and tack on the new Areas. This is how we use inheritance in Go for code reuse.
type OpenCardboardBox struct {
CardboardBox
}
And now we get to use an Open Cardboard Box and an Cardboard Box. They both implement the Solid interface so we can use interface's like in any other OO language.
func (this *OpenCardboardBox) SurfaceArea() float {
return(this.CardboardBox.SurfaceArea() + 2 * (this.l * this.h) + 2 * (this.w * this.h))
}
One of the Main Differences between this and a true inheritance based implementation is that there is NO "is a" relationship in our chain. In the situation where we would need to check whether (OpenCardboardBox is a RectPrism) we would have to implement the "is a" relationship ourselves. The code might not be as pretty as that of a classic OO based language, but then again how often do we need the "is a" relationship?
func main() {
fmt.Printf("\n\n");
cbox := new(CardboardBox)
cbox.l = 2
cbox.w = 4
cbox.h = 2
cbox.isSoggy = true
obox := new(OpenCardboardBox)
obox.l = 2
obox.w = 4
obox.h = 2
obox.isSoggy = true
var rprism Solid = cbox
fmt.Printf(" Volume: %f\n", rprism.Volume())
fmt.Printf("Surface Area: %f\n", rprism.SurfaceArea())
rprism = obox
fmt.Printf(" Volume: %f\n", rprism.Volume())
fmt.Printf("Surface Area: %f\n", rprism.SurfaceArea())
fmt.Printf("\n\n");
}
Labels:
go,
golang,
howto,
inheritance,
Object Orientation,
tutorial
Subscribe to:
Posts (Atom)