go How to use various import

  • 2020-06-23 00:40:04
  • OfStack

go various import

The simplest:


package main 
import "fmt"
func main() {
  fmt.Println("hehe")  // ok
}

Needless to say.

Special use of imports

So, what does it mean to take 1 dot ? Take a look at:


package main 
import . "fmt"
func main() {
  Println("hehe")  // ok
}

As you can see, with dots, you can omit the package name when invoked.

Let's keep going. What does the underscore mean?


package main 
import _ "fmt"
func main() {
  fmt.Println("hehe")  // error
}

The underscore means that only fmt's init The fmt package variables and functions cannot be used, so the above program error.

Here's another one:


package main 
import x "fmt"
func main() {
  x.Println("hehe")  // ok
}

As you can see, this is equivalent to an alias.

Simple, no need to say more.

conclusion


Related articles: