These are chat archives for gin-gonic/gin

5th
Jul 2016
Armando Leon
@manofthelionarmy
Jul 05 2016 08:14
Hey everyone, I'm trying to apply Google Charts into my project. Here is the url: https://developers.google.com/chart/interactive/docs/quick_start
I understand doing templates with html, but I'm confused with how to do it in JavaScript. Or is it even possible to do that in JavaScript.

Also, here is my code in golang: package main

import (
//"database/sql"
//"log"
//"strconv"
"time"
//"github.com/coopernurse/gorp"
"github.com/gin-gonic/gin"
//_ "github.com/mattn/go-sqlite3"
"fmt"
"html/template"
)

type Data struct {
Time time.Time
Value float64
}

type DataPair struct {
Id string
Pair []Data
}

func createNode(t time.Time, v float64) *Data {

return &Data{t, v}

}

func initializePair(title string) *DataPair {

return &DataPair{Id: title}

}

func (dp *DataPair) addNode(node Data) {
dp.Pair = append(dp.Pair, node)
}

func (dp *DataPair) print() {
for i := range dp.Pair {
fmt.Printf("%s %.2f\n", dp.Pair[i].Time.String(), dp.Pair[i].Value)
}
}

func viewHandler(c *gin.Context) {

node := createNode(time.Now(), 72.4)

dp := initializePair("sample1")

dp.addNode(*node)

node = createNode(time.Now(), 75.3)

dp.addNode(*node)

t := template.Must(template.ParseFiles("view.html"))

t.Execute(c.Writer, dp)

}

func main() {

//dp.print()

router := gin.Default()
router.GET("/view/", viewHandler)

router.Run(":8080")

}

Thomas Boerger
@tboerger
Jul 05 2016 09:21
@manofthelionarmy you should properly format your code if you want somebody to help you ;)
Javier Provecho Fernandez
@javierprovecho
Jul 05 2016 11:25
    package main

    import (
        //"database/sql"
        //"log"
        //"strconv"
        "time"
        //"github.com/coopernurse/gorp"
        "github.com/gin-gonic/gin"
        //_ "github.com/mattn/go-sqlite3"
        "fmt"
        "html/template"
    )

    type Data struct {
        Time  time.Time
        Value float64
    }
    type DataPair struct {
        Id   string
        Pair []Data
    }

    func createNode(t time.Time, v float64) *Data {
        return &Data{t, v}
    }
    func initializePair(title string) *DataPair {
        return &DataPair{Id: title}
    }
    func (dp *DataPair) addNode(node Data) {
        dp.Pair = append(dp.Pair, node)
    }
    func (dp *DataPair) print() {
        for i := range dp.Pair {
            fmt.Printf("%s %.2f\n", dp.Pair[i].Time.String(), dp.Pair[i].Value)
        }
    }
    func viewHandler(c *gin.Context) {
        node := createNode(time.Now(), 72.4)

        dp := initializePair("sample1")

        dp.addNode(*node)

        node = createNode(time.Now(), 75.3)

        dp.addNode(*node)

        t := template.Must(template.ParseFiles("view.html"))

        t.Execute(c.Writer, dp)
    }
    func main() {
        //dp.print()

        router := gin.Default()
        router.GET("/view/", viewHandler)

        router.Run(":8080")
    }
Armando Leon
@manofthelionarmy
Jul 05 2016 16:03
@javierprovecho thanks!! I planned to do it this morning. How do you properly do that in this chat? Did you screenshot?