Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Define a new type of time in golang

Tags:

time

go

I want to have a struct like this:

type Person struct {
    Name string
    DateJoined time
}

But this struct will not compile, because there is no type time, isn't it? Should I alternatively use a string and insert the time / date information there?

like image 368
himekami Avatar asked Mar 03 '14 02:03

himekami


People also ask

What is type of time in Golang?

The operating system measures two types of time “Wall clock” time and “Monotonic” time. Wall clock time is used to tell time whereas monotonic clock time is used to measure time. Go time package provides functionalities to measure and manipulate both clocks.

Is there a date type in GO?

The time package in Go's standard library provides a variety of date- and time-related functions, and can be used to represent a specific point in time using the time. Time type. In addition to a time and date, it can also hold information about the time zone the represented date and time are in.


2 Answers

time isn't a type. time.Time is. See the package docs for the types: http://golang.org/pkg/time/

import time

type Person struct {
    Name string
    DateJoined time.Time
}
like image 97
elithrar Avatar answered Oct 12 '22 23:10

elithrar


you need to import time package and of course you use time.Time btw, it returned error when I defined my own type as below with similar reason to you. And, someone helped me to do cast (ex. mytime(time.Now()).

type mytime time.Time

You can make your own package and import always so that all your own type in your convenience

like image 26
GoGo Avatar answered Oct 13 '22 00:10

GoGo