Skip to content

Instantly share code, notes, and snippets.

@jayunit100
Created September 16, 2016 13:44
Show Gist options
  • Save jayunit100/72ac1eb5f5afa48c1f7c4719446ceff0 to your computer and use it in GitHub Desktop.
Save jayunit100/72ac1eb5f5afa48c1f7c4719446ceff0 to your computer and use it in GitHub Desktop.
jays snippets
package main
import (
"fmt"
"strings"
"strconv"
"os"
)
// How do you read a struct from a line of text?
type Example1 struct {
aString string
aNum int64
}
// input: a string
// output: a struct.
func example1_stringToStruct(in string) Example1 {
slice := StrSlice(strings.Split(in, ","))
aString := slice.Get(0)
aNum, _:= strconv.ParseInt(slice.Get(1),10,64)
// We can create a struct by relying on field ordering.
return Example1{
aString,
aNum,
}
}
// input: A map of ints->string
// output: A string array, where the map key == the index
func example2_mapToArray(m1 map[string]int) {
m := map[int]string{
1 : "a", 2 : "b",
}
v := make([]string, 0, len(m))
for _, value := range m {
v = append(v, value)
}
}
// inpt: a float
// output 1.0000 e+09
func example3_sciNot(i float64) string {
return fmt.Sprintf("sci: %e",i)
}
func example4_maxVals() string{
MaxUint := ^uint(0)
MaxInt := int(MaxUint >> 1)
MinInt := -MaxInt - 1
return fmt.Sprintf("%v %v %v ",MaxUint, MaxInt, MinInt)
}
// less intersting utilities...
// 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 ""
}
func files(basedir string) {
if err := os.MkdirAll(basedir,0777) ; err != nil {
panic("Cant create dir")
}
if _, err := os.Create(basedir +"/d.txt") ; err != nil {
panic("Cant write file to dir")
}
}
type Animal interface {
Speak() string
}
func breakMe() {
ints := []int{1,2,3,4}
Return:
for i,_ := range ints {
if i > 2 {
fmt.Print("breaking..")
break Return
}
}
}
func PrintAStringWithInts(i int) string {
return fmt.Sprintf("wave%v",strconv.Itoa(i))
}
// Function for detecting bad tags. We can post-process Its with this, and report them.
func getBadTags(description string) []string {
badTags := []string{}
validTags := []string{
"Feature",
"Measurement",
"Quirk",
}
for _,proposedTag := range splitBetweenChars(description,'[',']') {
valid := false
for _,aValidTag := range validTags {
// If this tag has one of the validTokens, its ok.
if strings.Contains(proposedTag,aValidTag) {
valid = true
}
}
if ! valid {
badTags = append(badTags, proposedTag)
}
}
return badTags
}
// splitBetweenChars used to split out tags i.e. "It [Feature:blah] [Flaky]"->string[]{"Feature:blah","Flaky"}
func splitBetweenChars(s string, start byte, end byte) []string {
entries := []string{}
indices := func(tmpStr string, border byte) []int {
ints := []int{}
for index,i := range tmpStr {
if byte(i) == border {
ints = append(ints,index)
}
}
return ints
}
starts := indices(s,'[')
ends := indices(s,']')
for i,startLoc := range starts {
tag := s[startLoc+1:ends[i]]
entries = append(entries, tag)
}
return entries
}
func stringMutat() {
stringMA :="a"
stringMB := stringMA
stringMB = "ASDF"
fmt.Print(stringMA," shouldnt = ", stringMB)
}
// the wacky syntax of copy(dest src)
func swap(which int, strings []string) {
temp := make([]string, len(strings)-1)
copy(temp, strings[:which])
copy(temp[which:], strings[which+1:])
strings = temp
fmt.Println(strings)
}
func main() {
// fmt.Println(fmt.Sprintf(example1_stringToStruct("hello,100")))
fmt.Println(example1_stringToStruct("hello,100"))
fmt.Println(example3_sciNot(1000000000))
fmt.Println(example4_maxVals())
files("/tmp/go-examples-test/a/b/c")
fmt.Println(PrintAStringWithInts(1))
fmt.Print("bad tags == ")
fmt.Println(getBadTags("[Feature] [a] [blah] [Quirk:x] [b] blah blah [c]"))
stringMutat()
fmt.Println()
strings := []string{"a","b"}
fmt.Print(strings)
swap(0,strings)
fmt.Print(strings)
}
type Pill int
const (
Placebo Pill = iota
Aspirin
Ibuprofen
Paracetamol
Acetaminophen = Paracetamol
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment