Skip to content

Instantly share code, notes, and snippets.

@chischaschos
Last active August 29, 2015 14:06
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 chischaschos/f252e827c14edf37ef48 to your computer and use it in GitHub Desktop.
Save chischaschos/f252e827c14edf37ef48 to your computer and use it in GitHub Desktop.
Examples about how to do basic things in Go
package main
import (
"fmt"
"io/ioutil"
"os"
"./mypackage"
)
// The Bla1 example tests if the output matches
func ExampleBla1() {
fmt.Println("Bla")
// Output:
// Bla
}
func ExampleReadFile() {
// http://golang.org/pkg/io/ioutil/#WriteFile
writeError := ioutil.WriteFile("data.txt", []byte("holi"), os.ModePerm)
if writeError != nil {
panic(writeError)
}
// http://golang.org/pkg/io/ioutil/#ReadFile
bytes, readError := ioutil.ReadFile("data.txt")
if readError != nil {
panic(readError)
}
fmt.Println(bytes)
fmt.Println(string(bytes))
// Output:
// [104 111 108 105]
// holi
}
func ExampleFunctions() {
fmt.Println(mypackage.Mysplit("oh no"))
fmt.Println(mypackage.Myjoin(mypackage.Mysplit("oh no")))
// Output:
// [oh no]
// oh|no
}
package mypackage
import (
"strings"
)
func Mysplit(sentence string) []string {
// http://golang.org/pkg/strings/#Join
return strings.Split(sentence, " ")
}
func Myjoin(parts []string) string {
// http://golang.org/pkg/strings/#Split
return strings.Join(parts, "|")
}
@chischaschos
Copy link
Author

Run with go test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment