1.编译
go build test.go
2.运行
go run test.go
3.看帮助
go doc builtin go doc fmt/fnv4.运行测试
编写源代码even.go
/* this package is test for write package */ package even // check value is even value // if is even, return true, otherwise false func Even(value int) bool { return 0 == (value % 2) } func Odd(value int) bool { return 1 == (value % 2) }编写测试代码even_test.go
package even import ( "testing" ) func TestEvent(t *testing.T) { if !Even(2) { t.Log("2 should be odd") t.Fail() } } func TestOdd(t *testing.T) { if !Odd(3) { t.Log("3 should be odd") t.Fail() } }运行测试
go test
5.创建包
创建源代码文件,在$GOROOT下的src\pkg\utils\ustackm目录下创建文件ustack.go
/** simulate stack, capacity is 10 **/ package ustack import ( "errors" strConvUtils "strconv" ) const stackMax = 10 type UStack struct { index int datas [stackMax]int } func (s *UStack) Push(value int) (result int, err error) { if s.index > (stackMax - 1) { result = -1 err = errors.New("exceed max size") return } s.datas[s.index] = value result = s.index s.index++ return } func (s *UStack) Pop() (result int, err error) { if s.index < 1 { result = -2 err = errors.New("exceed min size") return } result = s.datas[s.index-1] s.index-- return } func (s *UStack) String() (str string) { for i := 0; i < s.index; i++ { str = str + "[" + strConvUtils.Itoa(i) + ":" + strConvUtils.Itoa(s.datas[i]) + "]" } return }注意包内的函数和类型能被外界访问,必须名字第一个字符大写
go build ustack.go
go install将ustack.a放到$GOROOT下的pkg\windows_386\utils\ustack目录下或pkg\linux_386\utils\ustack
创建包方法另外一种方法
在环境变量中加入GOPATH,指定对应包目录(可以有多个目录),在下面建立src目录,作为包的目录
编写对应的go文件,比如上例的src\ustack\UStack.go
在任何目录执行
go install ustack自动在对应的gopath目录下生成pkg目录
6.接口的写法
type IFactory interface { getRunApp() IRunApp } type AppTools struct { m_Factory FactoryImpl } type IRunApp interface { run() } type RunAppImpl struct { } type FactoryImpl struct { m_RunApp RunAppImpl } func (factory *FactoryImpl) getRunApp() IRunApp { return &factory.m_RunApp } func (runApp *RunAppImpl) run() { } func (appTools *AppTools) getFactory() IFactory { return &appTools.m_Factory }
7编译链接
8g 123.go 8l -o 123.out 123.8
8得到操作系统目录分隔符
filepath.Separator
9判断文件或目录是否存在
func (appTools *AppTools) pathExists(pathName string) bool { if appTools.judgeEmptyStr(&pathName) { return false } _, err := ioutil.ReadDir(pathName) if nil != err { return false } return true } func (appTools *AppTools) fileExists(fileName string) bool { f, err := os.Open(fileName) if err != nil && os.IsNotExist(err) { return false } defer f.Close() return true }
10.得到日期
func (appTools *AppTools) getCurrentTime() string { return time.Now().Format("2006-01-02 15:04:05") }
11.字符串转为小写
strings.ToLower
12.拷贝文件函数
func (appTools *AppTools) copyFile(src, dst string) bool { srcFile, err := os.Open(src) if err != nil { appTools.log(fmt.Sprintln("Open", src, "failed", err.Error())) return false } defer srcFile.Close() dstFile, err := os.Create(dst) if err != nil { appTools.log(fmt.Sprintln("create", dst, "failed", err.Error())) return false } defer dstFile.Close() w, err := io.Copy(dstFile, srcFile) w = w + 1 if nil != err { appTools.log(fmt.Sprintln("Copy", src, "to", dst, "failed", err.Error())) return false } return true }
13.截取字符串
func (appTools *AppTools) substr(str string, start, count int) string { rs := []rune(str) rl := len(rs) end := 0 if start < 0 { start = 0 } if start > rl { return "" } if start < 0 { start = 0 } if start > rl { start = rl } end = start + count if end > rl-1 { end = rl } return string(rs[start:end]) }
14.读写文件
srcFile, err := os.OpenFile(hName, os.O_RDONLY, 0666) if nil != err { appTools.log(fmt.Sprintln("file", hName, "open failed", err.Error())) return } defer srcFile.Close() tempFile, err := os.OpenFile(tempFileName, os.O_APPEND|os.O_RDWR, 0666) if nil != err { appTools.log(fmt.Sprintln("file", tempFileName, "open failed", err.Error())) return } defer tempFile.Close() srcReader := bufio.NewReader(srcFile) destWriter := bufio.NewWriter(tempFile) defer destWriter.Flush() for { str, err := srcReader.ReadString('\n') if nil != err { if io.EOF != err { appTools.log(err.Error()) } break } _, err = destWriter.WriteString(str) if nil != err { appTools.log(err.Error()) } } }
15.字符串包含子字符串
// 包含 strings.Contains(str, substr) // 在结尾 strings.HasSuffix(str, substr) // 在头 strings.HasPrefix(str, substr)
有疑问加站长微信联系(非本文作者)