Golang 构建用于 HTML select 标签中选项的月份。

huangapple go评论91阅读模式
英文:

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 (
    &quot;fmt&quot;
)

var months = [12]string{
    &quot;January&quot;, &quot;February&quot;, &quot;March&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;,
    &quot;July&quot;, &quot;August&quot;, &quot;September&quot;, &quot;October&quot;, &quot;November&quot;, &quot;December&quot;,
}

func main(){
    for i, n := range months {
        fmt.Printf(&quot;%2d: %s\n&quot;, i, n)
    }
}

I would like to print out this:

&lt;option&gt;January&lt;/option&gt;
&lt;option&gt;February&lt;/option&gt;
&lt;option&gt;March&lt;/option&gt;
&lt;option&gt;April&lt;/option&gt;
&lt;option&gt;May&lt;/option&gt;
&lt;option&gt;June&lt;/option&gt;
&lt;option&gt;July&lt;/option&gt;
&lt;option&gt;August&lt;/option&gt;
&lt;option&gt;September&lt;/option&gt;
&lt;option&gt;October&lt;/option&gt;
&lt;option&gt;November&lt;/option&gt;
&lt;option&gt;December&lt;/option&gt;

答案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 (
	&quot;fmt&quot;
	&quot;time&quot;
)

func main() {
	for i := time.January; i &lt;= time.December; i++ {
		fmt.Printf(&quot;&lt;option&gt;%s&lt;/option&gt;\n&quot;, i)
	}
}

Output:

&lt;option&gt;January&lt;/option&gt;
&lt;option&gt;February&lt;/option&gt;
&lt;option&gt;March&lt;/option&gt;
&lt;option&gt;April&lt;/option&gt;
&lt;option&gt;May&lt;/option&gt;
&lt;option&gt;June&lt;/option&gt;
&lt;option&gt;July&lt;/option&gt;
&lt;option&gt;August&lt;/option&gt;
&lt;option&gt;September&lt;/option&gt;
&lt;option&gt;October&lt;/option&gt;
&lt;option&gt;November&lt;/option&gt;
&lt;option&gt;December&lt;/option&gt;

答案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 (
    &quot;fmt&quot;
)

var months = [12]string{
    &quot;January&quot;, &quot;February&quot;, &quot;March&quot;, &quot;April&quot;, &quot;May&quot;, &quot;June&quot;,
    &quot;July&quot;, &quot;August&quot;, &quot;September&quot;, &quot;October&quot;, &quot;November&quot;, &quot;December&quot;,
}

func main() {
    for _, month := range months {
        fmt.Printf(&quot;&lt;option&gt;%s&lt;/option&gt;\n&quot;, 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.

huangapple
  • 本文由 发表于 2014年2月9日 12:47:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/21655408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定