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:
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?
- 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)
}