网络协议里面,很可能遇到自定义的封包,对应到c里面的是 typedef struct _PackageHeader { int headerLen; int timeStamp; short cmd }; 为了保证单字节对齐,可以加上编译选项 #pragma pack(1) 可惜的是,golang里面,没有这样的编译选项,猥琐的可参考的山寨代码如下 package main import ( "encoding/binary" "fmt" ) type TestSt
2.2以上的版本下载网络资源不完整无法更新.HttpURLConnection.getContentLength()获取的size跟下载下来的file的legth不等. 原因是:HttpURLConnection跟服务交互采用了"gzip"压缩.所以下载的fileLegth > HttpURLConnection.getContentLength(). 参考api: By default, this implementation of HttpURLConnection requ
在linux上想获取文件的元信息,我们需要使用系统调用lstat或者stat. 在golang的os包里已经把stat封装成了Stat函数,使用它比使用syscall要方便不少. 这是os.Stat的原型: func Stat(name string) (FileInfo, error) Stat returns a FileInfo describing the named file. If there is an error, it will be of type *PathError. 返
http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=22312037&id=3756923 一.struct Go语言中,也有struct,定义与C语言类似,举例说明如下: type person struct { name string age int } 上面就声明了一个结构体person,包含两个字段.可以如下来使用这个str
最近日志打印的时候需要打印状态码,但是因为interface的原因直接获取失败,http.Request里面的response不知道怎么使用,所以就自己重写writeheader,write来截取status type doneWriter struct { http.ResponseWriter done bool status int } func (w *doneWriter) WriteHeader(status int) { w.done = true w.status= status
package main import ( "fmt" "github.com/StackExchange/wmi" ) type Storage struct { Name string FileSystem string Total uint64 Free uint64 } type storageInfo struct { Name string Size uint64 FreeSpace uint64 FileSystem string } func get
一.Json和struct互换 (1)Json转struct例子: type People struct { Name string `json:"name_title"` Age int `json:"age_size"` } func JsonToStructDemo(){ jsonStr := ` { "name_title": "jqw" "age_size":12 } ` var people P
方法一: serialize 的标准方法: 使用gob 和 base64 或 base58. 方法二: 下面是自己实现的 serialize 方法,不推荐自己实现,应该用标准方法. 代码如下: package main import ( "bytes" "encoding/binary" "fmt" "log" "os" ) //this type represnts a record with three