122 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			122 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // For more tutorials: https://blog.learngoprogramming.com
 | |
| //
 | |
| // Copyright © 2018 Inanc Gumus
 | |
| // Learn Go Programming Course
 | |
| // License: https://creativecommons.org/licenses/by-nc-sa/4.0/
 | |
| //
 | |
| 
 | |
| package main
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"strings"
 | |
| )
 | |
| 
 | |
| func main() {
 | |
| 	// The names of your best three friends
 | |
| 	names := [3]string{
 | |
| 		"Einstein",
 | |
| 		"Tesla",
 | |
| 		"Shepard",
 | |
| 	}
 | |
| 
 | |
| 	// The distances to five different locations
 | |
| 	distances := [5]int{50, 40, 75, 30, 125}
 | |
| 
 | |
| 	// A data buffer with five bytes of capacity
 | |
| 	data := [5]byte{'H', 'E', 'L', 'L', 'O'}
 | |
| 
 | |
| 	// Currency exchange ratios only for a single currency
 | |
| 	ratios := [1]float64{3.14145}
 | |
| 
 | |
| 	// Up/Down status of four different web servers
 | |
| 	alives := [4]bool{true, false, true, false}
 | |
| 
 | |
| 	// A byte array that doesn't occupy memory space
 | |
| 	//
 | |
| 	// Don't do this:
 | |
| 	//   zero := [0]byte{}
 | |
| 	//
 | |
| 	// Do this (when you don't assign elements):
 | |
| 	var zero [0]byte
 | |
| 
 | |
| 	// =========================================================================
 | |
| 
 | |
| 	separator := "\n" + strings.Repeat("=", 20) + "\n"
 | |
| 
 | |
| 	fmt.Print("names", separator)
 | |
| 	for i := 0; i < len(names); i++ {
 | |
| 		fmt.Printf("names[%d]: %q\n", i, names[i])
 | |
| 	}
 | |
| 
 | |
| 	fmt.Print("\ndistances", separator)
 | |
| 	for i := 0; i < len(distances); i++ {
 | |
| 		fmt.Printf("distances[%d]: %d\n", i, distances[i])
 | |
| 	}
 | |
| 
 | |
| 	fmt.Print("\ndata", separator)
 | |
| 	for i := 0; i < len(data); i++ {
 | |
| 		// try the %c verb
 | |
| 		fmt.Printf("data[%d]: %d\n", i, data[i])
 | |
| 	}
 | |
| 
 | |
| 	fmt.Print("\nratios", separator)
 | |
| 	for i := 0; i < len(ratios); i++ {
 | |
| 		fmt.Printf("ratios[%d]: %.2f\n", i, ratios[i])
 | |
| 	}
 | |
| 
 | |
| 	fmt.Print("\nalives", separator)
 | |
| 	for i := 0; i < len(alives); i++ {
 | |
| 		fmt.Printf("alives[%d]: %t\n", i, alives[i])
 | |
| 	}
 | |
| 
 | |
| 	// no loop for zero elements
 | |
| 	fmt.Print("\nzero", separator)
 | |
| 	for i := 0; i < len(zero); i++ {
 | |
| 		fmt.Printf("zero[%d]: %d\n", i, zero[i])
 | |
| 	}
 | |
| 
 | |
| 	// =========================================================================
 | |
| 
 | |
| 	// you know how this works :) don't be freaked out!
 | |
| 	fmt.Printf(`
 | |
| 
 | |
| %s
 | |
| FOR RANGES
 | |
| %[1]s
 | |
| 
 | |
| `, strings.Repeat("~", 30))
 | |
| 
 | |
| 	fmt.Print("names", separator)
 | |
| 	for i, v := range names {
 | |
| 		fmt.Printf("names[%d]: %q\n", i, v)
 | |
| 	}
 | |
| 
 | |
| 	fmt.Print("\ndistances", separator)
 | |
| 	for i, v := range distances {
 | |
| 		fmt.Printf("distances[%d]: %d\n", i, v)
 | |
| 	}
 | |
| 
 | |
| 	fmt.Print("\ndata", separator)
 | |
| 	for i, v := range data {
 | |
| 		// try the %c verb
 | |
| 		fmt.Printf("data[%d]: %d\n", i, v)
 | |
| 	}
 | |
| 
 | |
| 	fmt.Print("\nratios", separator)
 | |
| 	for i, v := range ratios {
 | |
| 		fmt.Printf("ratios[%d]: %.2f\n", i, v)
 | |
| 	}
 | |
| 
 | |
| 	fmt.Print("\nalives", separator)
 | |
| 	for i, v := range alives {
 | |
| 		fmt.Printf("alives[%d]: %t\n", i, v)
 | |
| 	}
 | |
| 
 | |
| 	// no loop for zero elements
 | |
| 	fmt.Print("\nzero", separator)
 | |
| 	for i, v := range zero {
 | |
| 		fmt.Printf("zero[%d]: %d\n", i, v)
 | |
| 	}
 | |
| }
 |