├── README.md ├── .gitignore ├── Makefile ├── exec └── exec_test.go ├── LICENSE └── var_dump └── var_dump_test.go /README.md: -------------------------------------------------------------------------------- 1 | go-samples 2 | ========== 3 | 4 | Samples for Go language. 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | libraries = exec var_dump 2 | 3 | .PHONY: all/% format/% test/% 4 | 5 | all: $(foreach library,$(libraries),all/$(library)) 6 | 7 | all/%: % go_get 8 | cd "$*"; go build 9 | 10 | test: $(foreach library,$(libraries),test/$(library)) 11 | 12 | test/%: % go_get 13 | cd "$*"; go test 14 | 15 | format: $(foreach library,$(libraries),format/$(library)) 16 | 17 | format/%: % 18 | cd "$*"; gofmt -d=true -tabs=false -tabwidth=2 -w=true . 19 | 20 | go_get: 21 | go get github.com/imos/go/var_dump 22 | -------------------------------------------------------------------------------- /exec/exec_test.go: -------------------------------------------------------------------------------- 1 | package exec 2 | 3 | import ( 4 | "bufio" 5 | "os/exec" 6 | "testing" 7 | ) 8 | 9 | func TestSimpleRead(t *testing.T) { 10 | command := exec.Command("echo", "hoge", "piyo") 11 | command_output, _ := command.Output() 12 | actual := string(command_output) 13 | expected := "hoge piyo\n" 14 | if actual != expected { 15 | t.Errorf("Command line output should be %#v, but %#v.", expected, actual) 16 | } 17 | } 18 | 19 | func TestInteraction(t *testing.T) { 20 | command := exec.Command("cat") 21 | raw_stdin, _ := command.StdinPipe() 22 | stdin := bufio.NewWriter(raw_stdin) 23 | raw_stdout, _ := command.StdoutPipe() 24 | stdout := bufio.NewReader(raw_stdout) 25 | command.Start() 26 | for i := 0; i < 3; i++ { 27 | size, _ := stdin.WriteString("abc\n") 28 | if size != 4 { 29 | t.Errorf("Output size should be 4, but %#v.", size) 30 | } 31 | stdin.Flush() 32 | actual, _ := stdout.ReadString('\n') 33 | expected := "abc\n" 34 | if actual != expected { 35 | t.Errorf("Output should be %#v, but %#v.", expected, actual) 36 | } 37 | } 38 | raw_stdin.Close() 39 | } 40 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Kentaro IMAJO 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /var_dump/var_dump_test.go: -------------------------------------------------------------------------------- 1 | package var_dump 2 | 3 | import ( 4 | "github.com/imos/go/var_dump" 5 | "testing" 6 | ) 7 | 8 | type TestStruct struct { 9 | Key int32 10 | PublicAttribute []string 11 | private_attribute *string 12 | Child *TestStruct 13 | } 14 | 15 | func TestVarDumpExport(t *testing.T) { 16 | foo := "foo" 17 | data := TestStruct{ 18 | Key: 12345, 19 | PublicAttribute: []string{"hoge", "piyo"}, 20 | private_attribute: &foo, 21 | Child: &TestStruct{ 22 | Key: 23456, 23 | PublicAttribute: []string{}, 24 | private_attribute: nil, 25 | Child: nil, 26 | }, 27 | } 28 | actual := var_dump.Export(data) 29 | expected := "" + // For gofmt 30 | "var_dump.TestStruct{\n" + 31 | " Key: int32(12345),\n" + 32 | " PublicAttribute: []string{\n" + 33 | " string(\"hoge\"),\n" + 34 | " string(\"piyo\"),\n" + 35 | " },\n" + 36 | " private_attribute: &string(\"foo\"),\n" + 37 | " Child: &var_dump.TestStruct{\n" + 38 | " Key: int32(23456),\n" + 39 | " PublicAttribute: []string{},\n" + 40 | " private_attribute: (*string)nil,\n" + 41 | " Child: (*var_dump.TestStruct)nil,\n" + 42 | " },\n" + 43 | "}" 44 | if actual != expected { 45 | t.Errorf("Output should be %#v, but %#v.", expected, actual) 46 | } 47 | } 48 | --------------------------------------------------------------------------------