英文:
Using new and assigning variable at the same time
问题
以下是翻译好的内容:
wd := new(time.Weekday)
fmt.Println(wd.String())
上述两行代码返回 Sunday(星期从0开始计算)
我是否可以在 new 后面赋值?我尝试的另一种方法是:
var wd time.Weekday
wd = 3
这个方法返回 Wednesday(星期三)。
英文:
wd := new(time.Weekday)
fmt.Println(wd.String())
The above two lines return Sunday (weekdays start with a 0)
Is it possible for me to assign a value along with new ? Other method i tried is
var wd time.Weekday
wd = 3
this one returns Wednesday
答案1
得分: 1
你可以简单地使用time.weekday
常量来实现:
wd := time.Wednesday
英文:
you can simply use the time.weekday constants for that:
wd := time.Wednesday
答案2
得分: 0
time.Weekday 是一个整数,所以你可以将其分配为整数类型(或者像Adam建议的那样使用定义的常量)。我可以问一下为什么你需要在这种情况下使用 new 吗?
package main
import (
"fmt"
"time"
)
func main() {
var wd time.Weekday = 3
fmt.Println(wd)
}
英文:
time.Weekday is an int so you can assign it as such (or use the defined constants as Adam suggested). Can I ask why you need to use new in this situation?
package main
import (
"fmt"
"time"
)
func main() {
var wd time.Weekday = 3
fmt.Println(wd)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论