Skip to content

Instantly share code, notes, and snippets.

@alwqx
Last active February 7, 2017 10:16
Show Gist options
  • Save alwqx/b8da904e6c77a84434e41be6a266f754 to your computer and use it in GitHub Desktop.
Save alwqx/b8da904e6c77a84434e41be6a266f754 to your computer and use it in GitHub Desktop.
golang deal with text
package main
import(
"strings"
"fmt"
)
func main() {
lines := "194 Temperature_Celsius 0x0022 037 045 000 Old_age Always - 37 (0 6 0 0 0)"
useSplit(lines)
useFields(lines)
}
func useSplit(lines string) {
line := strings.Split(lines, " ")
fmt.Println(line)
fmt.Println(len(line))
}
func useFields(lines string) {
line := strings.Fields(lines)
fmt.Println(line)
fmt.Println(len(line))
}
/*
output
➜ gist go run main.go
[194 Temperature_Celsius 0x0022 037 045 000 Old_age Always - 37 (0 6 0 0 0)]
42
[194 Temperature_Celsius 0x0022 037 045 000 Old_age Always - 37 (0 6 0 0 0)]
15
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment