I attempt to save the food selection in a data source and also load a Go html design template with this. I have actually taken care of to bring the JSON and also the dental filling of Go html design template. Independent of each various other. However just how do I move the JSON from the func primary
right into the design template func web page
?
plan primary.
import (.
" encoding/json".
" fmt".
" html/template".
" io/ioutil".
" net/http".
" os".
).
func primary() {
resp, err:= http.Get(" https://api3.go4webdev.org/menu/all").
if err!= nil {
fmt.Println(" No reaction").
}
postpone resp.Body.Close().
body, err:= ioutil.ReadAll( resp.Body).
fmt.Println( string( body))// just how pass this to "func web page" and also inhabit the food selection?
web page().
}
func web page() {
fmt.Println().
t:= template.Must( template.New(""). Analyze( templ)).
m:= map[string] user interface {} {}
if err:= json.Unmarshal([] byte( jsondata), && m); err!= nil {
panic( err).
}
if err:= t.Execute( os.Stdout, m); err!= nil {
panic( err).
}
}
const templ='.
<< html><> < body>>.
<< ul>>.
<< li id= {{. menu_id}} > > {{. menu_txt}} <.
<< li id= {{. menu_id}} > > {{. menu_txt}} <.
<.
< '.
const jsondata='{"menu_id":" 1", "menu_txt": "Residence"} '.
And also the outcome will certainly be this:
[{"menu_icn":"","menu_id":"code","menu_lnk":"/code","menu_main":"code","menu_sort":1,"menu_txt":"Code Philosophy"},{"menu_icn":"","menu_id":"home","menu_lnk":"/home","menu_main":"home","menu_sort":1,"menu_txt":"Navigation”}, etc…]
<< html><> < body>>.
<< ul>>.
<< li id= 1>> Residence<.
<< li id= 1>> Residence<.
<.
<.
When I attempt to pass the JSON information right into func web page( jsondata)
...
...
web page( string( body)).
}
func web page( jsondata) {
...
... I obtain some mistakes
/ main.go:23:11: undefined: jsondata
/ main.go:28:34: undefined: jsondata
And also if I include string after jsondata
web page( string( body)).
}
func web page( jsondata string) {
I obtained this mistakes:
can not unmarshal variety right into Go worth of kind map[string] user interface {}
.