Skip to content

Instantly share code, notes, and snippets.

@amitt001
Created March 10, 2016 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitt001/8d345425501341111c20 to your computer and use it in GitHub Desktop.
Save amitt001/8d345425501341111c20 to your computer and use it in GitHub Desktop.
Go Notes
/*
STRING OPERATIONS
package strings
*/
exStr := "Hello World"
strings.Contains(exStr, "lo") //returns true/false
strings.Index(exStr, "lo") //returns 3
strings.Count(exStr, "o") //returns 2
strings.Replace(exStr, "l", "_", 3) //here first 3 l's is replaced with _
strings.Fields(exStr) //splits on space
strings.Split(exStr, "o") //splits on a given character i.e splits on "o"
strArr := []string {"c", "b", "a"}
strings.Join(strArr, "-")//joins the array values with - seperator
//package sort
sort.Strings(strArr) //sorts the string
//package strconv
strInt := "100"
strconv.ParseInt(str, 0, 8)//string 100->int 100, 8: size of char is 8 bits
/*
FILE IO
packages
fmt
os
log
io/ioutil
*/
file, err := os.Create("sampa.txt")
if err != nil{
log.Fatal(err)
}
file.WriteString("Amit")
file.Close()
stream, err := ioutil.ReadFile("sampa.txt")
if err != nil{
log.Fatal(err)
}
fmt.Println(string(stream))
/*
HTTP server
package net/http
*/
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
func handler(w http.ResponseWriter, r *http.Request){
fmt.Println(w, "hello world")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment