- Hands-On Full Stack Development with Go
- Mina Andrawos
- 606字
- 2021-07-02 12:33:30
Functions – accessing functions from other packages
Earlier in this chapter, we covered the concept of packages, and the fact that a Go program is composed of a number of connected packages. So, how do we really connect packages? We connect packages by having the ability to call functions and retrieve types from other packages. But then comes the question, how do we expose a function to other packages?
In Go, there are no private or public keywords like in most other statically typed programming languages. If you want your function to be public, all you need to do is start your function name with an upper case letter. In Go, that is known as making your function exported. If, on the other hand, your function starts with a lower case letter, then your function is considered unexpected.
To absorb the preceding two paragraphs, let's go through some code. Here is a package called adder, which contains a single function called Add:
package adder
func Add(a,b int)int {
return a+b
}
Now, let's say we want to call Add from a different package. Here is what we'd do:
package main
//get the adder package
import "adder"
func main() {
result := adder.Add(4, 3)
//do something with result
}
In the preceding code, we called the exported function Add from our main package, at our main function. We did two things:
- Used the import keyword to load the adder package
- In the main function, we called adder.Add(..)
As demoed, to call an exported function, you need to use the following syntax:
<package name>.<exported function name>
If in the adder package we had named our function add instead of Add, the preceding code would not have worked. This is because when the function starts with a lower case letter it would be considered unexpected, which in effect means that it will be invisible to other packages.
Let's see a couple of examples from Go's standard package.
A very popular package from the Go standard packages is the fmt package. The fmt package can write to the standard output of your environment. It can also format strings and scan data from the standard input, among other things. Here is a simple but very commonly used code snippet:
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello Go world!!")
}
In the preceding code, we called a function called Println, which lives inside the fmt package. The Println function will take your string message and print it on the standard output. The output of the preceding program is as follows:
Hello Go world!!
Another popular package in the world of Go is math/rand, which we can use to generate random numbers. As we mentioned in the Packages section, earlier in this chapter, the reason why the package name is not just rand is simply because the rand package folder exists underneath the folder of the math package. So, even though rand is more of a sub-package, we just use the package name when we need to call exported functions that belong to it. Here is a simple example:
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println("Let's generate a random int", rand.Intn(10))
}
In the preceding code, we imported two packages—the fmt package and the math/rand package. We then invoked two functions from each of the packages. We first invoked the Println function, which belongs to the fmt package, to output a string to the standard output. Then, we invoked the Intn function, which belongs to the math/rand package, to generate a random number between zero and nine.
Now, let's take a look at what constitutes closures.
- 多媒體CAI課件設(shè)計(jì)與制作導(dǎo)論(第二版)
- Developing Mobile Web ArcGIS Applications
- Rust編程從入門到實(shí)戰(zhàn)
- oreilly精品圖書:軟件開發(fā)者路線圖叢書(共8冊(cè))
- INSTANT MinGW Starter
- MySQL數(shù)據(jù)庫管理與開發(fā)(慕課版)
- JavaScript+Vue+React全程實(shí)例
- Expert Data Visualization
- Nginx實(shí)戰(zhàn):基于Lua語言的配置、開發(fā)與架構(gòu)詳解
- Node.js Design Patterns
- Jenkins Continuous Integration Cookbook(Second Edition)
- C++從入門到精通(第5版)
- Hands-On Full Stack Development with Spring Boot 2.0 and React
- 工業(yè)機(jī)器人離線編程
- Fastdata Processing with Spark