Адриана обнови решението на 02.11.2015 17:35 (преди над 2 години)
+package main
+
+import (
+ "fmt"
+ "strings"
+)
+
+func ExtractColumn(logContents string, column uint8) string {
+ lines := getLines(logContents)
+ parts := getParts(lines)
+
+ return getColumn(column, parts)
+}
+
+func getLines(logContents string) []string {
+ lines := strings.Split(logContents, "\n")
+
+ for i, element := range lines {
+ if element == "" {
+ lines = append(lines[:i], lines[i+1:]...)
+ }
+ }
+ return lines
+}
+
+func getParts(lines []string) [][]string {
+ parts := make([][]string, len(lines))
+ for j, line := range lines {
+ parts[j] = strings.SplitN(line, " ", 4)
+ }
+ return parts
+}
+
+func getColumn(column uint8, parts [][]string) string {
+ var result string
+
+ if column == 0 {
+ for _, part := range parts {
+ result = fmt.Sprintf("%s%s %s\n", result, part[0], part[1])
+ }
+ } else if column == 1 {
+ for _, part := range parts {
+ result = fmt.Sprintf("%s%s\n", result, part[2])
+ }
+ } else if column == 2 {
+ for _, part := range parts {
+ result = fmt.Sprintf("%s%s\n", result, part[3])
+ }
+ } else {
+ result = "There is no such column!"
+ }
+ return result
+}