Created
March 15, 2016 13:16
-
-
Save jayunit100/be6b7b69e3c234301745 to your computer and use it in GitHub Desktop.
go snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"strings" | |
"strconv" | |
) | |
// How do you read a struct from a line of text? | |
type IPerfResult struct { | |
date string | |
// ... | |
bandwidthBits int64 | |
} | |
func main() { | |
in := "20160314154239,172.17.0.3,34152,172.17.0.2,5001,3,0.0-10.0,33843707904,27074774092" | |
slice := StrSlice(strings.Split(in, ",")) | |
if len(slice) != 9 { | |
// Failf("Too many fields in the output: %v",slice) | |
} | |
parts := IPerfResult{ | |
slice.Get(0), | |
slice.Get(1), | |
intOrFail("client port",slice.Get(2)), | |
slice.Get(3), | |
intOrFail("server port",slice.Get(4)), | |
slice.Get(5), | |
slice.Get(6), | |
intOrFail("transfer port",slice.Get(7)), | |
intOrFail("bandwidth port",slice.Get(8)), | |
} | |
fmt.Println(parts) | |
} | |
// Here's how to add a function to a primitive. | |
type StrSlice []string | |
func (s StrSlice) Get(i int) string { | |
if i >= 0 && i < len(s) { | |
return s[i] | |
} | |
return "" | |
} | |
// Example of how to parse an int | |
value, err := strconv.ParseInt(rawValue,10,64) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment