Table of Contents
|
installation
1) get the zip file , extra to a directory, for instance ~/go
2) create a directory to be your go projects work space. for instance ~/goworkspace, and mkdir ~/goworkspace/src
then set up
GOROOT=~/go
GOPATH=~/goworkspace
PATH=$PATH:$GOROOT/bin:$GOPATH/bin
program structure
http://golang.org/doc/code.html
you should refer here: create project structure
Testing
1) set up gmock and assert
http://godoc.org/code.google.com/p/gomock/gomock
http://whizdumb.me/2015/01/21/unit-testing-in-golang/
cd $GOPATH
go get code.google.com/p/gomock/gomock <-- this will fetch files from remote, put them under $GOPATH/src and build the object/archive files under $GOPATH/pkg/xxx_amd64/..
go install code.google.com/p/gomock/mockgen <-- this will build and install mockgen in $GOPATH/bin directory
go get github.com/stretchr/testify/assert
2) go files of business logic
- the $GOPATH/src/xyz.com/nova/sample/sample.go is like:
package sample
type MyInterface interface {
Fetch() string
}
type MyDb struct {
v int
}
func NewMyDb() * MyDb {
return &MyDb{v: 10}
}
func (* MyDb) Fetch() string {
return "world"
}
func Append(s string) string {
return s + " HAHAH"
}
- the main exectable file $GOPATH/src/xyz.com/nova/nova_runapp/nova_main.go , the directory name nova_runapp will be the actual executable file name when you do go install.
package main
import "fmt"
import "xyz.com/nova/sample" // IMPORTANT
func main() {
db := sample.NewMyDb()
fmt.Println(sample.Append(db.Fetch()))
}
to build and run the program
cd $GOPATH
go install xyz.com/nova/nova_runapp // the file nova_runapp will be put under $GOPATH/bin
go build xyz.com/nova/nova_runapp
go run src/xyz.com/nova/nova_runapp/nova_main.go
or
cd $GOPATH/src/xyz.com/nova/nova_runapp
go run nova_main.go
go build
go install
3) use gmock to generate mock object.
cd $GOPATH
$GOPATH/bin/mockgen -source=$GOPATH/src/xyz.com/nova/sample/sample.go > $GOPATH/src/xyz.com/nova/sample/sample_mock.go
then change the package in sample_mock.go from "sample_mock" to "package sample"
the sample_mock.go looks like :
// Automatically generated by MockGen. DO NOT EDIT!
// Source: src\sample\sample.go
package sample
import (
gomock "code.google.com/p/gomock/gomock"
)
// Mock of MyInterface interface
type MockMyInterface struct {
ctrl *gomock.Controller
recorder *_MockMyInterfaceRecorder
}
// Recorder for MockMyInterface (not exported)
type _MockMyInterfaceRecorder struct {
mock *MockMyInterface
}
func NewMockMyInterface(ctrl *gomock.Controller) *MockMyInterface {
mock := &MockMyInterface{ctrl: ctrl}
mock.recorder = &_MockMyInterfaceRecorder{mock}
return mock
}
func (_m *MockMyInterface) EXPECT() *_MockMyInterfaceRecorder {
return _m.recorder
}
func (_m *MockMyInterface) Fetch() string {
ret := _m.ctrl.Call(_m, "Fetch")
ret0, _ := ret[0].(string)
return ret0
}
func (_mr *_MockMyInterfaceRecorder) Fetch() *gomock.Call {
return _mr.mock.ctrl.RecordCall(_mr.mock, "Fetch")
}
4) create a test file $GOPATH/src/sample/sample_test.go with package sample
package sample
import (
"fmt"
"testing"
"code.google.com/p/gomock/gomock"
"github.com/stretchr/testify/assert"
)
func TestAppend(t *testing.T) {
t.Logf("the test passed!")
fmt.Println("test SHOULD be passed!")
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockObj := NewMockMyInterface(mockCtrl)
mockObj.EXPECT().Fetch().Return("xyz")
r := Append(mockObj.Fetch())
fmt.Println("the result is:", r)
assert.Equal(t, "xyz HAHAH1", r, "Not a good value")
}
5) run the test
cd $GOPATH
go test -v sample // here sample is the package, -v means show verbose information
go test -v sample -run ^TestAppend$ // if you want to run a single test case
or you can right click the file sample_test.go in IntelliJ to run the test. (ctrl-shift-f10 )
the whole project is here
http://manoftoday.wikidot.com/local--files/go-set-up/nova.zip
ide
IntelliJ IDEA 14.1 or later
download and install intellij community version
https://www.jetbrains.com/idea/download/
I would like to change the default font in Darcula scheme.
the steps:
from IntelliJ File--> Settings... -> Editor / Colors & Fonts / Font
for the scheme Darcula , save as "Darcula copy"
then i make adjust the font for editor, size 20, line spacing 0.8
and I also change the font for Editor / Colors & Fonts / Console Font to the same.
Go Plugin for Intellij
1) install plugin
https://github.com/go-lang-plugin-org/go-lang-idea-plugin
from IntelliJ File--> Settings... -> Plugins, click on Browse repository..., then Manage repositories...
use the alpha url from above link: https://plugins.jetbrains.com/plugins/alpha/5047
then install Go plug in, my version is 0.9.325
after the installation, you need to restart Intellij
2) config
from IntelliJ File--> Settings... -> Languages & Frameworks --> Go Libraries,
add your $GOPATH folder into global libraries IF "use GOPATH that is defined in system variable" is checked and the actual GOPATH is not listed in global libraries section.
ref: https://github.com/go-lang-plugin-org/go-lang-idea-plugin/wiki/v1.0.0-Setup-initial-project
3) create project structure
suppose your company is called xyz.com, and the new project is called nova, which will have an executable main app and a package called sample. the sample
will be used within the project nova or other projects inside or outside of xyz.com.
Thus, the executable main app refer the sample package as "../sample"; other projects inside xyz.com refer the sample package as "xyz.com/nova/sample";
projects outside of xyz.com can refer the package as "xyz.com/nova/sample".
And you put the executable file as $GOPATH/src/xyz.com/nova/nova_runapp/nova_main.go.
here is the best way to organize your project:
mkdir $GOPATH/src/xyz.com
mkdir $GOPATH/src/xyz.com/nova // create the nova project directory initially as IntelliJ won't create a directory for the project.
now when you create the project from IntelliJ, define project root as $GOPATH/src/xyz.com/nova and the project name nova.
from IntelliJ File--> New --> Project ... -> Go,
select SDK to $GOROOT path
note, in the intelliJ project nova, you DO NOT need src directory any more. However you need to perform the following actions inside IntelliJ (or manually do them inside a terminal):
right click the nova node in left panel:
- create directory: nova_runapp
- create go file: nova_main.go under nova_runapp directory
- create directory: sample
- create go file: sample.go and sample_test.go under sample directory
tree $GOPATH/src/xyz.com/
/home/..../program/goworkspace/src/xyz.com/
└── nova
├── nova.iml
├── nova_runapp
│ └── nova_main.go
└── sample
├── sample.go
├── sample_mock.go
└── sample_test.go
3 directories, 5 files
The whole $GOPATH directory content is like:
tree $GOPATH/
/home/master/program/goworkspace/
├── bin
│ ├── mockgen
│ └── nova_runapp
├── pkg
│ └── linux_amd64
│ ├── code.google.com
│ │ └── p
│ │ └── gomock
│ │ ├── gomock.a
│ │ └── mockgen
│ │ └── model.a
│ └── github.com
│ └── stretchr
│ └── testify
│ └── assert.a
└── src
├── code.google.com
│ └── p
│ └── gomock
│ ├── AUTHORS
│ ├── CONTRIBUTORS
│ ├── gomock
│ │ ├── call.go
│ │ ├── callset.go
│ │ ├── controller.go
│ │ ├── controller_test.go
│ │ ├── matchers.go
│ │ ├── matchers_test.go
│ │ └── mock_matcher
│ │ └── mock_matcher.go
│ ├── LICENSE
│ ├── mockgen
│ │ ├── mockgen.go
│ │ ├── model
│ │ │ └── model.go
│ │ ├── parse.go
│ │ └── reflect.go
│ ├── README.md
│ ├── sample
│ │ ├── imp1
│ │ │ └── imp1.go
│ │ ├── imp2
│ │ │ └── imp2.go
│ │ ├── imp3
│ │ │ └── imp3.go
│ │ ├── imp4
│ │ │ └── imp4.go
│ │ ├── mock_user
│ │ │ └── mock_user.go
│ │ ├── README.md
│ │ ├── user.go
│ │ └── user_test.go
│ └── update_mocks.sh
├── github.com
│ └── stretchr
│ └── testify
│ ├── assert
│ │ ├── assertions.go
│ │ ├── assertions_test.go
│ │ ├── doc.go
│ │ ├── errors.go
│ │ ├── forward_assertions.go
│ │ ├── forward_assertions_test.go
│ │ ├── http_assertions.go
│ │ └── http_assertions_test.go
│ ├── doc.go
│ ├── http
│ │ ├── doc.go
│ │ ├── test_response_writer.go
│ │ └── test_round_tripper.go
│ ├── LICENCE.txt
│ ├── mock
│ │ ├── doc.go
│ │ ├── mock.go
│ │ └── mock_test.go
│ ├── package_test.go
│ ├── README.md
│ ├── require
│ │ ├── doc.go
│ │ ├── forward_requirements.go
│ │ ├── forward_requirements_test.go
│ │ ├── requirements.go
│ │ └── requirements_test.go
│ └── suite
│ ├── doc.go
│ ├── interfaces.go
│ ├── suite.go
│ └── suite_test.go
└── xyz.com
└── nova
├── nova.iml
├── nova_runapp
│ ├── nova_main.go
│ └── nova_runapp
└── sample
├── sample.go
├── sample_mock.go
└── sample_test.go
36 directories, 60 files
Post preview:
Close preview