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 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算