Skip to content

Latest commit

 

History

History
59 lines (46 loc) · 1.41 KB

File metadata and controls

59 lines (46 loc) · 1.41 KB

1. Why do we use packages?

To avoid repetition and allow reuse. Packages also provide namespacing.

2. What is the difference between an identifier that starts with a capital letter and one which doesn't? (Average vs average)

An identifier that starts with a capital letter is visible outside the package. The identifiers starting with lower case letter can only be used/seen inside the same package.

3. What is a package alias? How do you make one?

Go allows us to rename a package using an alias. This is usally done to avoid naming conflicts.

4. We copied the average function from chapter 7 to our new package. Create Min and Max functions which find the minimum and maximum values in a slive of float64s.

package math

// Finds the average of a series of numbers
func Average(xs []float64) float64 {
	total := float64(0) 
	for _, x := range xs {
		total += x
	} 
	return total / float64(len(xs))
}

// Finds the minimum of a series of numbers
func Min(xs []float64) float64 {
	min := xs[0]
	for _, x := range xs {
		if x < min {
			min = x
		}
	}
	return min
}

// Finds the maximum of a series of numbers
func Max(xs []float64) float64 {
	max := xs[0]
	for _, x := range xs {
		if x > max {
			max = x
		}
	}
	return max
}

5. How would you document the functions you created in #4?

Executing: godoc golang-book/chapter11/math Min and godoc golang-book/chapter11/math Max