Last active
September 8, 2017 06:58
-
-
Save alextanhongpin/c05a8e59156b8d5b5e16c17bc0ca6d46 to your computer and use it in GitHub Desktop.
Useful go tips
This file contains 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" | |
) | |
func main() { | |
var a []string= []string{"a", "b"} | |
var b []string = []string{"c", "d"} | |
fmt.Println("Hello, playground", append(a, b...)) | |
} |
This file contains 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" | |
"math" | |
"strconv" | |
) | |
func main() { | |
n := int64(14) | |
v := string(strconv.FormatInt(n, 2)) | |
for k, v := range Reverse(v) { | |
if string(v) != "0" { | |
fmt.Println(math.Pow(2, float64(k))) | |
} | |
} | |
} | |
func Reverse(s string) string { | |
r := []rune(s) | |
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 { | |
r[i], r[j] = r[j], r[i] | |
} | |
return string(r) | |
} |
This file contains 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
// Code to write to json file | |
outjson, _ := json.Marshal(job) | |
err := ioutil.WriteFile("output.json", outjson, 0644) | |
log.Println(err) | |
// Go doesn't have math.round | |
// Multiple tags is not supported |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment