Wednesday, April 22, 2015

Im using the rpcbotinterfaceobjects from Day 4 in $GOPATH\src\github.com\<yourgithubusername>\rpcbotinterfaceobjects
If you dont have rpcbotinterfaceobjects_input.go and rpcbotinterfaceobjects_output.go please check my Day 4 post.

For RPC we need a new struct which connects the in&output structs to the RPC service:

Create a new file rpcservice.go in $GOPATH\src\github.com\<yourgithubusername>\rpcbotinterfaceobjects and copy into it:

Friday, April 17, 2015

Today i want to check if my understandings of Go packages are correct:

1) There is only one package inside a subdirectory under $GOPATH
2) The package name has to be the same as the subdirectory name
3) The package can consist of any number of files
4) The filenames of the *.go files inside one package can be anything you choose

So i created a new directory for my test library package:

$GOPATH\src\github.com\<yourgithubusername>\rpcbotinterfaceobjects
which is in my case:D:\goworkspace\src\github.com\kimxilxyong\rpcbotinterfaceobjects

Inside that directory i created two files:

1) rpcbotinterfaceobjects_input.go

packagerpcbotinterfaceobjects

typeBotInputstruct{

Contentstring

}

func(this*BotInput)GetContent()string{

returnthis.Content

}

func(this*BotInput)SetContent(NewContentstring){

this.Content=NewContent

}

2) rpcbotinterfaceobjects_output.go

packagerpcbotinterfaceobjects

typeBotOutputstruct{

Contentstring

}

func(this*BotOutput)GetContent()string{

returnthis.Content

}

func(this*BotOutput)SetContent(NewContentstring){

this.Content=NewContent

}

As you can see the package names are both the same, but the filenames can be anything you choose.

Next is to create the executable which uses this package in:

$GOPATH\src\github.com\<yourgithubusername>\packagetest
or in my case:D:\goworkspace\src\github.com\kimxilxyong\packagetest

Create a file named test_my_package.go (or actually any name you want) and insert the following code into it:

Run go install to build the executable, it will be put into your $GOPATH\bin folder.

Note: The bin will be named after your folder, not your *.go file!

Run packagetest

So far my assumptions 1-4 have been confirmed and additionally my tests seem to indicate that if you compile a file with func main() in it, the resulting exe is named like the directory your main comes from.

Maybe some of you may ask why I used this in the struct methods. Thats because I come from a Java and Delphi background and as this or self is not a keyword in Go I just used it for better readability.

Quote:Golang does not have a self or this keyword to reference to the current instance. In the method example of func (c *Circle) area() float64 the receiver struct is named "c". Use that variable name, rather than this to refer to the current instance.oo-example-in-golang

Create the following path: $GOPATH\src\github.com\<yourgithubusername>

The github subdirectory is not mandatory but is considered best practice and will make things easier for you in the future if you want to store your source on github. You dont need to have a github account at this time, just use a username that you would be using later on.

There are different opinions out there on the interwebs, but as far as I have googled it, it is recommended that you have only ONE workspace. That means all your own source and 3rd party source is all together in one workspace. I will adhere to this recommendation for now, but would be glad to read your opinions and/or arguments about this question.