How to truncate a string in a Golang template

后端 未结 7 613
轮回少年
轮回少年 2021-01-01 09:16

In golang, is there a way to truncate text in an html template?

For example, I have the following in my template:

{{ range .SomeContent }}
 ....
             


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-01 10:02

    str := "xxxx"
    n := 2
    if len(str) > n {
        fmt.Println(str[:n])
    }
    

    lest say we need quarter of ascii string

    str[:len(str)/4]
    

提交回复
热议问题