-
Book Overview & Buying
-
Table Of Contents
Go Programming - From Beginner to Professional - Second Edition
By :
Constants are like variables, but you can’t change their initial values. These are useful for situations where the value of a constant doesn’t need to or shouldn’t change when your code is running. You could make the argument that you could hardcode those values into the code and it would have a similar effect. Experience has shown us that while these values don’t need to change at runtime, they may need to change later. If that happens, it can be an arduous and error-prone task to track down and fix all the hardcoded values. Using a constant is a tiny amount of work now that can save you a great deal of effort later.
Constant declarations are similar to var statements. With a constant, the initial value is required. Types are optional and inferred if left out. The initial value can be a literal or a simple statement and can use the values of other constants. Like var, you can declare multiple constants in one statement. Here are the notations:
constant <name> <type> = <value> constant ( <name1> <type1> = <value1> <name2> <type2> = <value3> … <nameN> <typeN> = <valueN> )
In this exercise, we have a performance problem: our database server is too slow. We are going to create a custom memory cache. We’ll use Go’s map collection type, which will act as the cache. There is a global limit on the number of items that can be in the cache. We’ll use one map to help keep track of the number of items in the cache. We have two types of data we need to cache: books and CDs. Both use the ID, so we need a way to separate the two types of items in the shared cache. We need a way to set and get items from the cache.
We’re going to set the maximum number of items in the cache. We’ll also use constants to add a prefix to differentiate between books and CDs. Let’s get started:
main.go file to it.main.go, add the main package name to the top of the file:package main
import "fmt"
const GlobalLimit = 100
MaxCacheSize constant that is 10 times the global limit size:const MaxCacheSize int = 10 * GlobalLimit
const ( CacheKeyBook = "book_" CacheKeyCD = "cd_" )
map value that has a string value for a key and a string value for its values as our cache:var cache map[string]string
func cacheGet(key string) string {
return cache[key]
}func cacheSet(key, val string) {MaxCacheSize constant to stop the cache going over that size: if len(cache)+1 >= MaxCacheSize {
return
}
cache[key] = val
}func GetBook(isbn string) string {return cacheGet(CacheKeyBook + isbn) }
func SetBook(isbn string, name string) {cacheSet(CacheKeyBook+isbn, name) }
func GetCD(sku string) string {CD cache prefix to create a unique key:return cacheGet(CacheKeyCD + sku) }
func SetCD(sku string, title string) {CD cache prefix constant to build a unique key for the shared cache:cacheSet(CacheKeyCD+sku, title) }
main() function:func main() {map value:cache = make(map[string]string)
SetBook("1234-5678", "Get Ready To Go")CD cache prefix to the cache: SetCD("1234-5678", "Get Ready To Go Audio Book")Book from the cache: fmt.Println("Book :", GetBook("1234-5678"))CD from the cache: fmt.Println("CD :", GetCD("1234-5678"))main() function:}
go run .
The following is the output:
Figure 1.22: Output displaying the Book and CD caches
In this exercise, we used constants to define values that don’t need to change while the code is running. We declared then using a variety of notation options, some with the typeset and some without. We declared a single constant and multiple constants in a single statement.
Next, we’ll look at a variation of constants for values that are more closely related.