Last active
August 29, 2015 14:06
-
-
Save chischaschos/f252e827c14edf37ef48 to your computer and use it in GitHub Desktop.
Examples about how to do basic things in Go
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" | |
"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 | |
} |
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 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, "|") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run with
go test