Skip to content

Build native mobile apps faster: Write logic in GO. Views in ReactNative.

License

Notifications You must be signed in to change notification settings

hemantasapkota/goma

Repository files navigation

Goma - ごま

Build native mobile apps faster by writing your application logic in GO

With Goma, you can write your mobile app's business logic in GO and reuse them across platforms like IOS and Android. It comes with an embedded database that allows you to save/restore application's objects.

Goma utilizes gomobile to generate platform specific libraries for your apps.

Requirements

  • At least Go 1.5.3 or above
  • Xcode
  • Android Studio or Eclipse

Installation

Get or update Goma with:

go get -u github.com/hemantasapkota/goma

Concepts & Usage

goma.Object - Definition & Construction

Anything that is persitable can be modeled with a Goma object. It can be your app's view, model or both.

A complete goma object should embed goma.Object and implement the goma.DBObject interface.

type Person struct {
  *goma.Object
   Name string `json:"name"`
   Age int `json:"age"`
}

// Implement goma.DBObject interface's Key() method
func (p *Person) Key() string {
  return "sampleApp.Person"
}

goma.Object - Saving to the database

person := &Peron{
 Name: "Jon Panda",
 Age: 20,
}

err := person.Save(person)
if err != nil {
 // do something
}

The object is serialized to JSON and stored in DB.

{"name":"Jon Panda","age":20}

goma.Object - Restoring from the database

person := &Person{}
err := person.Restore(person)

if err != nil {
 // do something
}

Note: For consistency purpose, it's recommended that GOMA object receivers be of pointer type. See this blog post for more details.

Marshaling - Return values from Goma

gomobile has some restrictions as to what types you can return. They are primitives ( int, int64, float float64, string, and bool ) and ( byte array ) []byte.

Goma leverages Go's excellent JSON support. Complex types like structs, arrays, and maps can be easily marshalled into JSON.

Synchronization of Objects

Synchronization is required to ensure multiple goroutine don't modify the same object at the same time. Using mutexes to sycnhronize read/write access to the objects can solve this problem.

type ContainerItem struct {
	Id int `json:"id"`
}

type Container struct {
	*Object
	sync.Mutex

	Children []ContainerItem
}

func (c *Container) Key() string {
	return "goma.container"
}

func (c *Container) AddChild(child ContainerItem) {
	c.Lock()
	defer c.Unlock()

	c.Children = append(c.Children, child)
}

Wiki

Examples

  • Meal Tracker - A simple meal tracker that I use personally

See https://github.com/hemantasapkota/goma-examples

Contribute

We would love for you to contribute to Goma, check the LICENSE file for more info.

Meta

Hemanta Sapkota

Distributed under the MIT license. See LICENSE for more information.

https://github.com/hemantasapkota