Importing packages is key to using a lot of the useful functionality available with Go. An import statement can be used with either single line syntax, or with a shortcut multi-line syntax.
Single Line example:
import "fmt"
import "os"
In the shortcut format, this is:
import
(
"fmt"
"os"
)
or:
import(
"fmt"
"os"
)
Notice that you don't have to put the opening parenthesis on the same line as the import keyword. They are not the same as curly braces.
This may not seem like much of a shortcut, but consider importing six or seven libraries (or more). Having to retype import all the time can get a bit tiresome, and is really unnecessary.
Another really cool thing about importing with Go is that the compiler won't let you import a package that you aren't using.
Consider the following:
package main
import
(
"os"
"fmt"
)
func main(){
fmt.Printf("How's it going there?");
}
This will generate an error and you can't compile the program. Why? Because you imported the os package and didn't use it.
No comments:
Post a Comment