10
func (
*ImageModel) GetImageListCount(project_id int64,paramter_optional ...int) int {
t int
expire_time := 600
len(paramter_optional) > 0 {
expire_time = paramter_optional[0]
}
...
|
go func() {
profServeMux := http.NewServeMux()
profServeMux.HandleFunc(
"/debug/pprof/"
"/debug/pprof/cmdline"
"/debug/pprof/profile"
"/debug/pprof/symbol"
err := http.ListenAndServe(
":7789"
err != nil {
panic(err)
}
接下来就可以使用go tool pprof分析。
如何进行程序调试:
对于调试,每个人理解不一样, 如果要调试程序功能, 重新编译即可, Golang的编译速度极快。如果在开发的时候调试程序逻辑, 一般用log即可, Golang里面最好用的log库是log4go, 支持log级别。如果要进行断点调试, GoEclipse之类的是支持的, 依赖Mingw和GDB, 我个人不习惯这种调试方法。
守护进程(daemon)
下面给出完整的真正可用的例子:
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
package main
import (
"fmt"
"log"
"os"
"runtime"
"syscall"
"time"
)
func daemon(nochdir,noclose int) int {
ret,ret2 uintptr
err syscall.Errno
darwin := runtime.GOOS ==
"darwin"
// already a daemon
syscall.Getppid() == 1 {
0
}
// fork off the parent process
ret,ret2,err = syscall.RawSyscall(syscall.SYS_FORK,0)
err != 0 {
-1
}
// failure
ret2 < 0 {
os.Exit(-1)
}
// handle exception for darwin
darwin && ret2 == 1 {
ret = 0
}
// if we got a good PID,then we call exit the parent process.
ret > 0 {
os.Exit(0)
}
/* Change the file mode mask */
_ = syscall.Umask(0)
// create a new SID for the child process
s_ret,s_errno := syscall.Setsid()
s_errno != nil {
log.Printf(
"Error: syscall.Setsid errno: %d"
}
s_ret < 0 {
-1
}
nochdir == 0 {
os.Chdir(
"/"
)
}
noclose == 0 {
f,e := os.OpenFile(
"/dev/null"
e == nil {
fd := f.Fd()
syscall.Dup2(int(fd),int(os.Stdin.Fd()))
}
}
0
}
func main() {
daemon(0,1)
{
fmt.Println(
"hello"
)
time.Sleep(1 * time.Second)
}
进程管理:
个人比较喜欢用supervisord来进行进程管理,支持进程自动重启,supervisord是一个python开发的工具, 用pip安装即可。
代码热更新:
代码热更新一直是解释型语言比较擅长的,Golang里面不是做不到,只是稍微麻烦一些, 就看必要性有多大。如果是线上在线人数很多, 业务非常重要的场景, 还是有必要, 一般情况下没有必要。
- 更新配置.因为配置文件一般是个json或者ini格式的文件,是不需要编译的, 在线更新配置还是相对比较容易的, 思路就是使用信号, 比如SIGUSER2, 程序在信号处理函数中重新加载配置即可。
- 热更新代码.目前网上有多种第三方库, 实现方法大同小异。先编译代码(这一步可以使用fsnotify做到监控代码变化,自动编译),关键是下一步graceful restart进程,实现方法可参考:http://grisha.org/blog/2014/06/03/graceful-restart-in-golang/也是创建子进程,杀死父进程的方法。
条件编译:
条件编译时一个非常有用的特性,一般一个项目编译出一个可执行文件,但是有些情况需要编译成多个可执行文件,执行不同的逻辑,这比通过命令行参数执行不同的逻辑更清晰.比如这样一个场景,一个web项目,是常驻进程的, 但是有时候需要执行一些程序步骤初始化数据库,导入数据,执行一个特定的一次性的任务等。假如项目中有一个main.go,里面定义了一个main函数,同目录下有一个task.go函数,里面也定义了一个main函数,正常情况下这是无法编译通过的, 会提示“main redeclared”。解决办法是使用go build 的-tags参数。步骤如下(以windows为例说明):
1.在main.go头部加上// +build main
2. 在task.go头部加上// +build task
3. 编译住程序:go build -tags 'main'
4. 编译task:go build -tags 'task' -o task.exe
官方说明:
Build Constraints
A build constraint is a line comment beginning with the directive +build that lists the conditions under which a file should be included in the package. Constraints may appear in any kind of source file (not just Go),but they must appear near the top of the file,preceded only by blank lines and other line comments.
To distinguish build constraints from package documentation,a series of build constraints must be followed by a blank line.
如果将项目有关资源文件打包进主程序:
使用go generate命令,参考godoc的实现。
与C/C++ 交互
1.Cgo,Cgo支持Golang和C/C++混编, 在Golang里面使用pthread,libuv之类的都不难,github上也有相关开源代码;
2.Swig, 很多库都用Swig实现了Golang的绑定,Swig也可以反向回调Golang代码。
3. syscall包, 该包让你以Golang的方式进行系统编程,不需要再使用C/C++,syscall提供了很多系统接口,比如epoll,原始socket套接字编程接口等。
其他:
近几年最热门的技术之一Docker是用Golang开发的, 已经有相关的书出版, 对系统运维,云计算感兴趣的可以了解。
来自:http://blog.csdn.net/yxw2014/article/details/43451625
相关资讯 —
更多
- golang(go语言) 1.4.1 发布
- 使用Go语言一段时间的感受
- Go 语言的演化历程
- 编程能力七段论
- Docker Hub 官方仓库发布编程语言包
- Go 编程语言刚发布了 Go 1 released了!
- 2014年各种编程语言的薪资和市场需求
- 谷歌 Go 编程语言首个候选版发布
- 跟踪最热门的Github repos,每日更新
- Go 1.4 RC1 发布
- Go 语言开发工具 LiteIDE x23 发布
- 技术人必读:19本免费指南性编程书籍
- 将会改变未来IT世界的十种编程语言
- 全球数据显示Go语言在中国最流行
- Go 语言的高级并发模式
- 适用于 Go 1.0 版的 Google App Engine SDK 发布
- 高效能程序员的七个习惯
- 轻量级脚本语言,fakescript 1.1 发布
- Go语言的10个实用技术
- 为什么开发人员从Java转Go
|
|
相关文档 —
更多
- Dive into Golang.pdf
- golang web开发.pdf
- Go Web 编程.pdf
- 一个Python老兵眼中的golang.pdf
- Effective Go(中文版).pdf
- Wide 用户指南.pdf
- VC编程经验总结.chm
- Ruby 经验总结.doc
- LoadRunner 性能测试经验总结.pdf
- DB2 调优经验总结.doc
- OSCache使用经验总结.doc
- SQL Sever2005 优化经验总结.pdf
- hadoop2.2+hbase0.96+hive0.12 安装整合详细高可靠文档及经验总结.pdf
- QTP 学习与实践经验总结.doc
- qtp学习与实践经验总结1.doc
- 大型信息系统工程建设经验总结.doc
- 从学校出来一路狂奔一年多的工作对于SSH项目的一些经验总结.pdf
- JDBC连接数据库经验总结 .doc
- Hive开发经验问答式总结.pdf
- C#经典项目经验总结.doc
|
同类热门经验
- Go语言的fmt包中文教程
6953次浏览
- Golang测试技术
3374次浏览
- Go语言(golang)开源项目大全
13299次浏览
- Go语言开发工具 LiteIDE
6481次浏览
- GO语言标准库概览
5415次浏览
- 顺手的Go语言编辑器
8380次浏览
|