Golang: Import With a Dots Prefix & Alias

As we know, to access functions / struct / variables that are in other packages, the package name needs to be written

Import With a Dots Prefix

As we know, to access functions / struct / variables that are in other packages, the package name needs to be written, for example as in the use of library.Studentand fmt.Println().

In Go, the components in other imported packages can be leveled with the import package component, by adding a dot ( .) after writing keywords import.

The point of the level here is that all properties in other imported packages can be accessed without the need to write the package name, such as when accessing something from the same file.
import ( . "belajar-golang-level-akses/library" "fmt" ) func main() { var s1 = Student{"ethan", 21} fmt.Println("name ", s1.Name) fmt.Println("grade", s1.Grade) }

In the code above the package is libraryimported using a dot. With that, struct calling is Student not necessary by writing the package name.

. . .

Utilization of Alias ​​When Importing a Package

Functions that are in other packages can be accessed by writing package names followed by the name of the function, for example like fmt.Println().

The package that has been imported can be renamed by using an alias at the time of import. An example can be seen in the following code.
import ( f "fmt" ) func main() { f.Println("Hello World!") }

In the above code, the package has an fmt alias f, to access Println() it with enough f.Println().
. . .

Never miss a post from Prasad Patil, when you sign up for Ednsquare.