mirror of
https://github.com/bootdotdev/fcc-learn-golang-assets.git
synced 2025-12-10 07:11:19 +00:00
1.0 KiB
1.0 KiB
Custom Package
Let's write a package to import and use in hellogo.
Create a sibling directory at the same level as the hellogo directory:
mkdir mystrings
cd mystrings
Initialize a module:
go mod init {REMOTE}/{USERNAME}/mystrings
Then create a new file mystrings.go in that directory and paste the following code:
// by convention, we name our package the same as the directory
package mystrings
// Reverse reverses a string left to right
// Notice that we need to capitalize the first letter of the function
// If we don't then we won't be able access this function outside of the
// mystrings package
func Reverse(s string) string {
result := ""
for _, v := range s {
result = string(v) + result
}
return result
}
Note that there is no main.go or func main() in this package.
go build won't build an executable from a library package. However, go build will still compile the package and save it to our local build cache. It's useful for checking for compile errors.
Run:
go build