英文:
Golang Construct Months for options in html select tag
问题
我刚刚开始学习Golang,并且想要按顺序列出月份,以便在HTML的select标签中使用选项:
我已经开始了以下代码:
package main
import (
"fmt"
)
var months = [12]string{
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
}
func main(){
for i, n := range months {
fmt.Printf("<option>%s</option>\n", n)
}
}
我想要打印出以下内容:
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
英文:
I have just started learning Golang and would like to list months in order for a options in an html select tag:
I have started this:
package main
import (
"fmt"
)
var months = [12]string{
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
}
func main(){
for i, n := range months {
fmt.Printf("%2d: %s\n", i, n)
}
}
I would like to print out this:
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
答案1
得分: 3
例如,
package main
import (
"fmt"
"time"
)
func main() {
for i := time.January; i <= time.December; i++ {
fmt.Printf("<option>%s</option>\n", i)
}
}
输出:
<option>一月</option>
<option>二月</option>
<option>三月</option>
<option>四月</option>
<option>五月</option>
<option>六月</option>
<option>七月</option>
<option>八月</option>
<option>九月</option>
<option>十月</option>
<option>十一月</option>
<option>十二月</option>
英文:
For example,
package main
import (
"fmt"
"time"
)
func main() {
for i := time.January; i <= time.December; i++ {
fmt.Printf("<option>%s</option>\n", i)
}
}
Output:
<option>January</option>
<option>February</option>
<option>March</option>
<option>April</option>
<option>May</option>
<option>June</option>
<option>July</option>
<option>August</option>
<option>September</option>
<option>October</option>
<option>November</option>
<option>December</option>
答案2
得分: 0
你已经接近成功了:
package main
import (
"fmt"
)
var months = [12]string{
"一月", "二月", "三月", "四月", "五月", "六月",
"七月", "八月", "九月", "十月", "十一月", "十二月",
}
func main() {
for _, month := range months {
fmt.Printf("<option>%s</option>\n", month)
}
}
唯一棘手的部分是使用 _
而不是 i
,以避免在构建程序时出现错误“i declared and not used”。
_
被称为 空白标识符,你可以在 Go 文档 中了解更多信息。
英文:
You are nearly there:
package main
import (
"fmt"
)
var months = [12]string{
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December",
}
func main() {
for _, month := range months {
fmt.Printf("<option>%s</option>\n", month)
}
}
The only tricky part is to use _
instead of i
, to avoid an error "i declared and not used" when you try to build your program.
_
is called the blank identifier, you can learn more in the go documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论