github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume1/section3/declarearrays/declarearrays.go (about)

     1  // Some examples of array declarations and showing how arrays are treated as values in Go.
     2  package main
     3  
     4  import "fmt"
     5  
     6  // 1st attempt at populating the integer array
     7  func populateIntegerArray(input [5]int) {
     8  
     9  	input[0] = 3
    10  	input[1] = 6
    11  	input[2] = 9
    12  	input[3] = 12
    13  	input[4] = 15
    14  
    15  }
    16  
    17  // 2nd attempt at populating the integer array
    18  func populateIntegerArrayWithReturnValue(input [5]int) [5]int {
    19  
    20  	input[0] = 3
    21  	input[1] = 6
    22  	input[2] = 9
    23  	input[3] = 12
    24  	input[4] = 15
    25  	return input
    26  }
    27  
    28  func beatlesArrayExample() {
    29  
    30  	// Declare and initialize values in an array
    31  	var beatles [4]string
    32  	beatles[0] = "John"
    33  	beatles[1] = "Paul"
    34  	beatles[2] = "Ringo"
    35  	beatles[3] = "George"
    36  	fmt.Printf("Beatles consists of: %v\n", beatles)
    37  	fmt.Println("The name of third band member in the beatles array is", beatles[2])
    38  
    39  	fmt.Println("Length of beatles: ", len(beatles))
    40  
    41  	// In Go, arrays are values. When we assign one array to another, all the elements from
    42  	// the array on the right hand side are copied over to the array on the left hand side.
    43  	var greatRockBandFromThe60s [4]string
    44  	greatRockBandFromThe60s = beatles
    45  	fmt.Printf("Members from a great rock band from the 1960s: %v\n", greatRockBandFromThe60s)
    46  
    47  	// Since arrays are values, equality comparisons of two arrays are done value for value.
    48  	// Note that the beatles array and the greatRockBandFromThe60s array have two different
    49  	// addresses in memory. The value comparison only checks the values of the arrays, and
    50  	// the memory address of the two arrays is not a criteria for the comparison.
    51  	fmt.Printf("beatles mem address: %p\n", &beatles)
    52  	fmt.Printf("greatRockBandFromThe60s mem address: %p\n", &greatRockBandFromThe60s)
    53  	if beatles == greatRockBandFromThe60s {
    54  		fmt.Println("The beatles array equals the greatRockBandFromThe60s array")
    55  	}
    56  }
    57  
    58  func u2ArrayExample() {
    59  
    60  	// Declare and initialize using the := operator. Instead of writing 4 lines of code
    61  	// to initialize the array, we get the job done in 1 line of code using an array
    62  	// literal value
    63  	u2 := [4]string{"Bono", "Edge", "Adam", "Larry"}
    64  	fmt.Printf("U2 consists of: %v\n", u2)
    65  	fmt.Println("The name of second band member in the u2 array is", u2[1])
    66  
    67  	fmt.Println("Length of u2: ", len(u2))
    68  }
    69  
    70  func main() {
    71  
    72  	// Declare an array of 5 integers
    73  	// Note: If we do not initialize a value for the array, they will default to the
    74  	// zero value of the type. In the case of integers, we expect the zero value to be 0.
    75  	var myArray [5]int
    76  	fmt.Printf("Contents of myArray: %v\n\n", myArray)
    77  
    78  	// Arrays are passed by value to functions, meaning a copy of the array is passed
    79  	// and not a reference (pointer) to the array.
    80  	populateIntegerArray(myArray)
    81  	fmt.Printf("Contents of myArray: %v\n\n", myArray)
    82  
    83  	myArray = populateIntegerArrayWithReturnValue(myArray)
    84  	fmt.Printf("Contents of myArray: %v\n\n", myArray)
    85  
    86  	// Use the built in len function to get the length of an array
    87  	fmt.Println("Length of myArray: ", len(myArray))
    88  
    89  	beatlesArrayExample()
    90  
    91  	u2ArrayExample()
    92  
    93  }