Welcome to part 2 of the Go programming tutorial series. In the previous tutorial, we got everything set up and ran our first basic program, along with explaining some of what is going on. Here, I plan to further explain some more of our options, and more of how to understand how Go is working and how it can work for you.
You're always going to start with the package information, which, for now, will just be
package main
Next you would do your imports. Most of the time, you wont have just a single package you want to import. Let's say you have two. You *could* do:
import "fmt" import "math"
But the style convention for Go Lang suggests you do the following instead:
import ( "fmt" "math" )
Now your full script might look like:
package main import ( "fmt" "math" ) func main() { fmt.Println("The square root of 4 is",math.Sqrt(4)) }
This time we're using the Sqrt
function from the math
library, which is short for square root, so it just returns the square root of the number we pass in the parameters. The parameters go inside the parenthesis.
Another question you might be already wondering, is how come the our main
function is running? We only are defining it here, we never asked it to run! In Go, the main function runs automatically. For example, if we did:
package main import ( "fmt" "math" ) func foo() { fmt.Println("The square root of 4 is",math.Sqrt(4)) } func main() { }
Nothing would happen, because we've only defined foo
, but we never call it to run, and our main function has nothing happening in it. To run foo(), we could run it from the main function:
package main import ( "fmt" "math" ) func foo() { fmt.Println("The square root of 4 is",math.Sqrt(4)) } func main() { foo() }
Another subtlety in Go is the casing of the functions. You'll notice that the functions we've made so far start with lower-case letters, but you might have noticed that all of the functions we've been using have upper-case letters to start. What's the deal there? It turns out this is actually an enforced standard in Go, where functions are either "exported" or "unexported". For a function to be used outside of its package (like how we're attempting to use math.Sqrt, but outside of the math package), it needs to be an "exported" function. If we imported the file we just made, we would not be able to use the foo
function, for example.
One more note for me to make about imports is when packages are contained within packages. So far, all of the functions we've used have been directly inside of the packages we've imported, but what if we want to access something like a random number? Well this could be done using the rand
package from within the math
package. In order to do this, we need to first import the rand
package, changing our import statement to:
import ( "fmt" "math/rand" )
So we can reference packages within packages with the forward slash. Now we can use the Intn
function from the rand
package like so:
rand.Intn(100)
All together now:
package main import ( "fmt" "time" "math/rand" ) func main() { fmt.Println("It is currently:",time.Now()) fmt.Println("A number from 1-100", rand.Intn(100)) }
If you're ever curious to know more about the function that you're using, you can use godoc
from your command line, like so:
godoc fmt Println
Which should return:
func Println(a ...interface{}) (n int, err error) Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.