1024programmer Java Go basic learning record – Web development blog article list display function

Go basic learning record – Web development blog article list display function

Every time I put my practice code on github and give it a tag to facilitate the use of students later, here I will share the practice with the code of the following branch

https://github.com/durban89/typescript_demo.git
 tag: 1.1.5

This sharing continues the previous Model layer. The previous Model layer reconstruction has finally been completed piecemeal. Today I will continue to add the article list display function. Although it is a simple list display, it is still It took a long time

The process is very simple, let’s briefly explain it first, and then explain the specific difficulties

Step one, add control layer

Add a controller function ArticleItem to display the list data of the called article and display it on the front end. The code is as follows

// ArticleItem list
 func ArticleItem(w http.ResponseWriter, r *http.Request) {
     var blogModel models.BlogModel

     var autokid int64
     var title string
     selectField := models.SelectValues{
         "autokid": &autokid,
         "title": &title,
     }

     where := models.WhereValues{}

     qr, err := blogModel.Query(selectField, where, 0, 10)

     if err != nil {
         fmt.Println(err)
         http.NotFound(w, r)
         return
     }

     t, err := template.ParseFiles(config.TemplateDir + "/item.html")

     if err != nil {
         http.Error(w, err.Error(), http.StatusInternalServerError)
         return
     }

     err = t.Execute(w, struct {
         Data[]models.SelectResult
     }{
         Data: qr,
     })

     if err != nil {
         http.Error(w, err.Error(), http.StatusInternalServerError)
         return
     }
 }

The second step, View layer modification

Here we need to add a view file templates/item.html, the code inside is as follows


  
    
    
    
    
    
  
  
  
    
      
        
          
           

From the Firehose

{{range .Data}}

{{ .autokid }} - {{ .title }} blog post

{{end}} RightSidebar

The third step, add route

Modify the routes/routes.go file as follows

{
     Path: "/item/",
     Fn: controllers.ArticleItem,
 },

After adding, recompile and access /item, you will see output similar to the following

Go basic learning record - Web development blog article list display function

The above is the output of the specific logic, but here we mainly talk about the difficulties

How to output array type data in a template, operate as follows

{{range .Data}}

 

{{ .autokid }} - {{ .title }} blog post

{{end}}

This can be found in the view file just now

And we also need to pass corresponding parameters in the control layer, as follows

err = t.Execute(w, struct {
     Data[]models.SelectResult
 }{
     Data: qr,
 })

The Model layer query function failed due to an error in the use of the reflection mechanism. It has been changed as follows, and the previous article will be modified accordingly

for k, v := range s {
     var ref = reflect.ValueOf(v)
     var refv = ref.Elem()

     if refv.Kind() == reflect.Int64 {
         tmpResult[k] = refv.Int()
     } else if refv.Kind() == reflect.String {
         tmpResult[k] = refv.String()
     }

     i++
 }

The main purpose here is to make a judgment and then convert the value to the corresponding type. Otherwise, unlike the previous calling method, what is actually assigned is just the address of a pointer. When the loop reaches the last one, all values ​​will be programmed with a value. This You will see that each set of values ​​in the obtained list is the same.

That’s it for today’s sharing. If you don’t understand the reflection mechanism very well, you can search it. I didn’t think golang was so popular before. In fact, as long as you search for a certain relevant knowledge point, you will find that many articles have been introduced in this article. relevant knowledge.

Project update address

https://github.com/durban89/typescript_demo.git
 tag: 1.1.6

The above is the entire content of this article. I hope that the content of this article can bring some help to everyone’s study or work, and I also hope that everyone will support us

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/749442

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索