How to build an application in organized and OO way

Hello!

This is my third day tinkering with Go and my past experience involves only interpreted languages with dynamic types such as PHP and Lua. So please be kind to me :smile:

http://play.golang.org/p/OURPXuZvjT
http://play.golang.org/p/zEWNJuWHU7

My “application” works with json data and stores them in memcache. I could create setters and getters for each gmcp data type, but it doesn’t feel right. I tried to use interfaces{} type, but id was a mess as well. I had to assert each time and do some other guessing.

Also is this the way how local packages are meant to be used?

Any good advice from you guys?

Also how can i insert code here in the forum? :stuck_out_tongue:

Tervist,

Three backticks or four spaces in front of code.

Anyways, first off, memcache is a cache not a database/datastore. So storing data there is probably not a good idea. sqlite, postgres bolt etc. are a better choice.

As a general rule, organize code by value. For example, since you seem to be writing a MUD, start by creating packages room, character, world. Then you will probably have some types such as room.Info, character.Info, character.Status, world.State etc. With some interfaces such as character.Store, room.Store, world.DB … It would be my starting point, and then overtime refine, add/merge/split packages as needed.

Write the MUD server first without any persistence, it will help you understand Go better. If you ensure that your whole world state is accessible from a single variable, then you can create backups by writing that value on to a disk with gob. Later it’s easier to find out how to properly persist and store it in a DB.

If you need telnet protocol implementation see here. I wrote it a while ago, and cleaned it up today, basic support should be there and it can support extensions, once I figure out what a nice API for it would look like.

I am not writing a mud. Just a small “wrapper” for the tt++ mud client. This json is the gmcp data from the mud server.

Only way i can use Go with tt++ is to call it like a command line script which takes arguments. So this is why i need the memcache to act like a data transfer server or something between the script executions. Did it make any sense?

But my question really was only about how to manage different json data types in a efficient way. Like should i have setters and getter for each gmcp data?

I don’t understand what you mean by “wrapper”. How would you use the program that you are writing?

Just making sure that you understand that if store a value in memcache, it might be gone when you try to access it. In other words, this is a completely valid behavior:

cache.Set(&memcache.Item{Key: "123456", Value: []byte("123")})
data, err := cache.Get("123456")
fmt.Println(data, err)
// prints: nil memcache: cache miss

And, I would manipulate the data directly, unless I have a reason not to. e.g.

room := db.Rooms.ByID(123)
room.Name = "Hello World"
db.Rooms.Update(123, room)

Without actually knowing how everything fit together, I don’t have anything simpler to suggest.

To manage json data types, take a look at the JSON package. It defines a standard way to encode/decode Go structs into/out of JSON. Also, you might wanna take a look at how to use interfaces in Go. They are powerful. REALLY powerful.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.