Go package selection

I'm trying to write an application to infer status from a database, but I seem to be fixated on a really basic language principle. I have a program written, but it does not compile due to the use of package time not in selector error.

Truly a basic example (from play.golang.org's own test environment)

 package main import ( "fmt" "time" ) func main() { s_str := time.Now() fmt.Println( printT(s_str) ) } func printT(t time) time { return t.Add(100) } 

Unfortunately, I found documentation and helpdocs on the Internet a little willing. I understand that the import statement must include the entire library for the entire program, for example, in C ++?

+7
go
source share
1 answer

You must prefix the imported types or variables with the name that you pointed to the package in the import (here you use the default name, that is, "time"). This is what you did for the Now function, but you should do it also for types.

Thus, the type is not time , but time.Time (that is, the type of time that is declared in the package you import with the name "time" ).

Change your function to

 func printT(t time.Time) time.Time { return t.Add(100) } 

And for your second question: No, the import statement does not include the library for the entire program, but only for the current file.

+23
source share

All Articles