Now that I come to a point to start with creating a rest-service I'm ready to go(lang).

I want to use an ORM to map go-structs to sqlite tables. For this I want to use xorm. Since I'm also new to golang there will be lots of things to wrap my head around.

type Song struct {
	Id   int64 
	Name string
	Comments []string
	Upvotes     int64
}

Here to mention:

  • field names should start with a capital letter
  • Id with int64 maps create this as autoincremented
    
import (
	"database/sql"
	"fmt"
	"os"
	"strconv"

	"github.com/go-xorm/xorm"

	_ "github.com/mattn/go-sqlite3"
)

    ...

    func dbcall() {
        f := "singleMapping.db" // remove sqllite on startup
        os.Remove(f)

        orm, err := xorm.NewEngine("sqlite3", f) // create db with mapping

		orm.ShowSQL(true) // show create sql
        err = orm.CreateTables(&Song{}) // create table for this type

		song := Song{}
        song.Name = ""
        song.Comments = []string{}
        _, err = orm.Insert(song) // insert song (id:0)
        
        song.Name = "aa" 
        _, err = orm.Insert(song) // change name and insert (id:1)
        song.Name = "bb"
        _, err = orm.Insert(song) // change name and insert (id:2)

        var allSongs []Song
        err = orm.Find(&allSongs) // retrieve all songs

        fmt.Println("---- SONGS ---")
        for _, song := range allSongs { 
            fmt.Println(song)
        }

        pSong := Song{}
        _, err = orm.ID(1).Get(&pSong) // retrieve single song with Id:1
        fmt.Println(song)}
  }  

Refs:
https://gobook.io/read/gitea.com/xorm/manual-en-US/
https://unknwon.io/posts/140616_xorm-go-orm-advanced-usage/
https://godoc.org/github.com/go-xorm/xorm
https://github.com/go-xorm/xorm/blob/master/examples