0

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)
} 
icza
  • 389,944
  • 63
  • 907
  • 827
mx88
  • 27
  • 1
  • 1
  • 7

1 Answers1

5

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\n", 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\n", 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

icza
  • 389,944
  • 63
  • 907
  • 827