#HTML template not populating properly

47 messages · Page 1 of 1 (latest)

topaz mesa
#

can you try
{{range $u := . }}
<li>{{$u}}</li>
{{end}}

i wrote it in mobile, sorry if i did typo

nimble prawn
#

change {{range .User_list}} to {{range .}}

topaz mesa
#

i can not test my option because i am on mobile but if it is wrong, i can delete it to prevent misinformation

nimble prawn
topaz mesa
#

because you pass slice directly instead of passing it in a struct

nimble prawn
#

Because you passed the argument directly, instead of wrap it in struct or map.

#

.User_list means takes value from field User_list of what ever you passed to Execute.

#

i prefer to have it in a separate file. also CSS use { and } (js too) quite a lot. I'm afraid it'll mess up with go template.

topaz mesa
#

i don't know if it works i have never used it but, what about passing content of a css file to template, and using that parameter with {{ }} inside <style> tags? Does it work?

#

please inform me whether it works or not. i am wondering it

#

awesome!! but as i said i have never used it. i don't know whether it is best practice

#

another idea i thought is that publishing css file on different endpoint, such as /mycss
and giving the path to style like

<head>
<link rel="stylesheet" href="/mycss">
</head>
#

may be you can use contexts

nimble prawn
#

yes, data race.

#

http.Server invoked the handler concurrently and you are using one global var which accessed by multiple goroutines without protection.

topaz mesa
#

or you can use closures

func userHandlerFunc(users []string) func(w http.ResponseWriter, r *httpRequest){
  return func(w http.ResponseWriter, r *httpRequest){
  fmt.Println(users)
  }
}

func Serve(users []string) {
  http.HandleFunc("/", userHandlerFunc(users))
  log.Fatal(http.ListenAndServe(":8080", nil))
}
#

but i don't understand that you call Serve function only once, don't you?

#

why don't you implement router/controller/repository pattern here? because when users are changed in DB, they won't reflect your router unless you restart app

#

it is simply like fetch users inside handler every time

#
type UserRepository interface{
  GetUsers() []string
}

type Router struct {
  userRepository UserRepository
}

func (r Router) userHandler(w http.ResponseWriter, r *httpRequest) {
  users := r.userRepository.GetUsers()
  fmt.Println(users)
}

func Serve() {
  router := Router{
    userRepository: NewUserRepository()
  }
  http.HandleFunc("/", router.userHandler)
  log.Fatal(http.ListenAndServe(":8080", nil))
}

#

just like it

#

its just an example ideally routers should not do DB operations they should just call controller/service methods, services make an repository call and then return it

#

i hope i could explain the approach

#

yes

#

how do you fetch users from DB

#

can you share code

#
type userRepositoryImpl struct {
  db *sql.DB
}

func NewUserRepository() UserRepository {
  db, err := sql.Open("sqlite3", "./db.db")
  Handle_error(err)
  return &userRepositoryImpl{db:db}
}

func (u *userRepository) GetUsers() []string {
   rows, err := u.db.Query("SELECT user_name FROM users;")
    Handle_error(err)
    defer rows.Close()

    var users []string

    for rows.Next() {
        var u string
        err := rows.Scan(&u)
        Handle_error(err)
        users = append(users, u)
    }

    return users
}

you can create repository like that

#

using interface here is best practice, by that way you can create mock repositories for unit testing

#
type UserRepository interface{
  GetUsers() []string
}

they are simply Method signatures for structs

#

may be you should complete go tour

umbral cairn
#

(usually they are structs, but you can define them on any type, even on primitives 😄 )

topaz mesa
nimble prawn
#

yes, maybe? not sure. but they seems to serve the same purpose.

#

traits allows you to embed funcs into an object and provide default implementation. Interface is simply a contract.

topaz mesa
#

if a struct implenting all methods of an interface, it automatically implements that interface just like Java if you familiar with Java.
In java you should use imlements keyword to make a class implements an interface, but in Go no need it.

#

actually an interface @umbral cairn mentioned is interface{} as you see it is empty interface, so every struct and primitives implement it automatically because it requires no method

#

let me quich search for you

topaz mesa
topaz mesa
nimble prawn
#
type a int
func (_ a)print() {}
type i interface {print()}
var _ i = a(0)

i believe this is what danslo means. an int (primitive) that implement an interface.

topaz mesa
#

yes that is i mentioned, but i supposed danslo means that kind of interfaces

func print(x interface{}) {
    fmt.Println(x)
}

func main() {
    print(5)
    print("mugetsu")
}
#

sorry if i misunderstand danslo

topaz mesa
#

but i see now what s/he means it sorry mb