dsam70528 2017-06-08 11:32
浏览 104
已采纳

Go-void函数和直接赋值

I have a little problem in Go language. I have this struct:

type Time struct{
    hour,min,sec int
}

And this function that initializes it:

func init_Time(t Time) (int,int,int){
    t.hour, t.min, t.sec = time.Now().Clock()
    return t.hour, t.min, t.sec
}

And the main is:

func main(){
   var Tm Time
   Tm.hour, Tm.min, Tm.sec = init_Time(Tm)
   fmt.Printf("Time: %d:%d:%d", Tm.hour, Tm.min, Tm.sec)
} 

I have imported time package too. It works perfectly but I have 2 questions about it:

  1. In my code, why is the assignment to the variables (hour,min,sec) twice done both in the init_Time function:

    t.hour, t.min, t.sec = time.Now().Clock()

and in the main():

Tm.hour, Tm.min, Tm.sec = init_Time(Tm)

Is it necessary or is my mistake?

  1. Why if I modify the init_Time function by transforming it into a void function, it returns values "Time: 0:0:0" instead of the current time?

(Example:)

func init_Time(t Time) {
      t.hour, t.min, t.sec = time.Now().Clock()
}
...
func main(){
     var Tm Time
     init_Time(Tm)
     fmt.Printf("Time: %d:%d:%d", Tm.hour, Tm.min, Tm.sec)
} 
  • 写回答

1条回答 默认 最新

  • doulian5857 2017-06-08 11:37
    关注

    Calling any function (or method) and passing values makes a copy of the values, and inside the function (or method) you can only modify the copy. Hence if you don't assign the return values in your first example to the fields of Time, changes made in init_Time() are lost when the function returns. This also answers your 2nd question.

    If you want the caller to observe the changes, you must pass a pointer to your value, and have the function modify the pointed value. In this case it is not even required to return the (modified) value:

    func InitTime(t *Time) {
      t.hour, t.min, t.sec = time.Now().Clock()
    }
    

    Using it:

    t := Time{}
    InitTime(&t)
    fmt.Printf("Time: %d:%d:%d
    ", t.hour, t.min, t.sec)
    

    Output (try it on the Go Playground):

    Time: 23:0:0
    

    (Note that the current time on the Go Playground always starts at 23:00:00.)

    Also it's idiomatic to create a "constructor" like function:

    func NewTime() *Time {
        t := &Time{}
        t.hour, t.min, t.sec = time.Now().Clock()
        return t
    }
    

    Using it:

    t2 := NewTime()
    fmt.Printf("Time: %d:%d:%d
    ", t2.hour, t2.min, t2.sec)
    

    Output is the same.

    See related questions:

    Golang Operator Overloading

    Copy instances of type T, when any of the methods of a named type T have a pointer receiver

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测