Files

67 lines
1.3 KiB
Go
Raw Permalink Normal View History

// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
2019-10-30 19:34:44 +03:00
// For more tutorials : https://learngoprogramming.com
// In-person training : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus
package main
import (
"fmt"
"strconv"
"strings"
)
func main() {
const (
header = "Location,Size,Beds,Baths,Price"
data = `New York,100,2,1,100000
New York,150,3,2,200000
Paris,200,4,3,400000
Istanbul,500,10,5,1000000`
separator = ","
)
var (
locs []string
sizes, beds, baths, prices []int
)
rows := strings.Split(data, "\n")
for _, row := range rows {
cols := strings.Split(row, separator)
locs = append(locs, cols[0])
n, _ := strconv.Atoi(cols[1])
sizes = append(sizes, n)
n, _ = strconv.Atoi(cols[2])
beds = append(beds, n)
n, _ = strconv.Atoi(cols[3])
baths = append(baths, n)
n, _ = strconv.Atoi(cols[4])
prices = append(prices, n)
}
for _, h := range strings.Split(header, separator) {
fmt.Printf("%-15s", h)
}
fmt.Printf("\n%s\n", strings.Repeat("=", 75))
for i := range rows {
fmt.Printf("%-15s", locs[i])
fmt.Printf("%-15d", sizes[i])
fmt.Printf("%-15d", beds[i])
fmt.Printf("%-15d", baths[i])
fmt.Printf("%-15d", prices[i])
fmt.Println()
}
}