Таня обнови решението на 23.10.2015 08:33 (преди над 2 години)
+package main
+
+import (
+ "bytes"
+ "strings"
+ "unicode/utf8"
+)
+
+//Ugly solution. Will rewrite.
+
+const COLUMN_COUNT int = 4
+
+func ExtractColumn(logContents string, column uint8) string {
+ rows := strings.Split(logContents, "\n")
+ var segmentBuffer bytes.Buffer
+ for _, row := range rows {
+ segmentBuffer.WriteString(getWantedRowSegment(row, column))
+ }
+ return segmentBuffer.String()
+}
+
+func getWantedRowSegment(row string, column uint8) string {
+ rowSegments := strings.SplitN(row, " ", COLUMN_COUNT)
+ if len(rowSegments) < COLUMN_COUNT {
+ return ""
+ }
+ rowSegment := getSegment(rowSegments, column)
+ if utf8.RuneCountInString(rowSegment) > 0 {
+ rowSegment += "\n"
+ }
+ return rowSegment
+}
+
+func getSegment(segs []string, column uint8) string {
+ switch column {
+ case 0:
+ return segs[0] + " " + segs[1]
+ default:
+ return segs[column+1]
+ }
+}