Visibility is the attribute of a function or a variable to be visible to different parts of the program. So a variable can be used only in the function that is declared, in the entire package or in the entire program.
How can I set the visibility of a variable or function? Well, it can be confusing at the beginning but it cannot be simpler:
Here you can see an example of a public
function:
package hello func Hello_world(){ println("Hello World!") }
Here, Hello_world
is a global function (a function that is visible across the entire source code and to third party users of your code). So, if our package is called hello
, we could call this function from outside of this package by using hello.Hello_world()
method.
package different_package import "github.com/sayden/go-design-patters/first_chapter/hello" func myLibraryFunc() { hello.Hello_world() }
As you can see, we are in the different_package
package. We have to import the package we want to use with the keyword import. The route then is the path within your $GOPATH/src
that contains the package we are looking for. This path conveniently matches the URL of a GitHub account or any other Concurrent Versions System(CVS) repository.
18.188.10.1