Time to review how to encapsulate methods.
- Go usually adopts CamelCases for naming.
- If the first letter is in upper case, this method is
- If the first letter is in lower case, this method is
privateis visible or not for
- Each folder is a package, unlike Java, the folder name is not necessarily the package name
- Each folder can only have one package
- main package contains the entry for execution
- All struct methods must be placed in the same package, yet could be saved in different files
Let’s reorgnize the folder as below:
Then we need to refactor the code as following:
The package is called
tree, it has two files and a sub-folder containing
entry point for
Note all values and functions are capitalized for public use.
Methods don’t have to be in the same file, we can create a
traverse.go file to hold
In the entry sub-folder, we have an
entry.go file. We have a sub-folder because each folder can only have one package.
Now run it,
We got the results as expected.