Array of structure and html templates.

114 views
Skip to first unread message

Victor Medrano

unread,
Aug 29, 2021, 10:01:54 PM8/29/21
to golang-nuts
I.m new to go, and working with a web  aplicación I need to pass a tray of struct to the template.
something like this 

type Month struct {
Mname string
Color string
Id int
}


type Year struct {
Yname, Color string
Selected bool
}

type Data struct {
Years []*Year
Months []*Month
}
but I haven found the way to fill data Data structure from mysql. 
I came to this group because I found it on a book.

so thanks.

Victor

Amit Saha

unread,
Aug 30, 2021, 1:33:16 AM8/30/21
to Victor Medrano, golang-nuts
Just to clarify, you want to first create an object of Data struct
type by querying data from a MySQL database and then passing the
object to a template where you will render a HTML page using the data?

If so, for the first step, you will use the database/sql package
(along with the mysql driver) to first create the struct, then execute
the template by passing the object to it. For templates, you may find
https://astaxie.gitbooks.io/build-web-application-with-golang/content/en/07.4.html
and https://echorand.me/posts/golang-templates/ useful.

>
> so thanks.
>
> Victor
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/ce221a92-ffec-48b5-b635-a0f29ffe932an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages