Go语言中文网 为您找到相关结果 30

go interface 转 string,解析json

package main import ( "encoding/json" "fmt" ) var user map[string]interface{} func main() { userJson := "{\"username\":\"system\",\"password\":\"123456\"}" json.Unmarshal([]byte(userJson), &user) fmt.Println(user) //打印结果:map[password:123456 username:system] username := user["username"] fmt.Println("username ==", username) //username == system //判断u...阅读全文

博文 2015-11-09 15:00:01 aqiang912

golang md5及随机字符串

package encrypt import ( "crypto/md5" "encoding/hex" "math/rand" "time" ) // 生成32位MD5 func MD5(text string) string{ ctx := md5.New() ctx.Write([]byte(text)) return hex.EncodeToString(ctx.Sum(nil)) } // return len=8 salt func GetRandomSalt() string { return GetRandomString(8) } //生成随机字符串 func GetRandomString(len int64) string{ str := "0123456789abcd...阅读全文

博文 2015-11-03 11:00:06 lcj0304

Go指南中的联系:Stringers

 练习:Stringers 让 IPAddr 类型实现 fmt.Stringer 以便用点分格式输出地址。 例如,IPAddr{1, 2, 3, 4} 应当输出 "1.2.3.4"。 下面是给出的参考模板 package main import "fmt" type IPAddr [4]byte // TODO: Add a "String() string" method to IPAddr. func main() { addrs := map[string]IPAddr{ "loopback": {127, 0, 0, 1}, "googleDNS": {8, 8, 8, 8}, } for n, a := range addrs { fmt.Printf("%v: %v\n", n...阅读全文

博文 2015-11-18 01:00:04 u013564276

Go语言zip解压.

package main import ( "archive/zip" "fmt" "io" "os" ) func main() { r, err := zip.OpenReader(`server_update_20151020.zip`) if err != nil { fmt.Println(err) return } for _, k := range r.Reader.File { if k.FileInfo().IsDir() { err := os.MkdirAll(k.Name, 0644) if err != nil { fmt.Println(err) } continue } r, err := k.Open() if err != nil { fmt.Println...阅读全文

博文 2015-10-20 11:00:35 fyxichen

golang语言中struct的初始化方式

// 先定义结构体 type Rect struct { width int height int } // 再初始化结构体 rect1 := new(Rect) rect2 := &Rect{} rect3 := &Rect{10, 20} rect4 := &Rect{width:10, height:20} // 定义 + 初始化同时进行 rect5 := &struct{width int, height int}{10, 20} 版权声明:本文为博主原创文章,未经博主允许不得转载...阅读全文

博文 2015-08-04 11:00:02 YanJiangbo

Go监控目录变化小程序.

package main import ( "flag" "log" "os/exec" "strings" "sync" "time" "github.com/go-fsnotify/fsnotify" ) var ( sleeptime int path string cmd string args []string ) func init() { flag.IntVar(&sleeptime, "t", 30, "-t=30") flag.StringVar(&path, "p", "./", "-p=filepath or dirpath") flag.StringVar(&cmd, "c", "", "-c=command") str := flag.String("a", "",...阅读全文

博文 2015-10-25 02:00:05 fyxichen

Go语言Slice操作.

1、基本用法: a = append(a, b...) 例如:list = appened(list,[]int{1,2,3,4}...) 可以用来合并两个列表. 不用这样了 :list := make([]int,len(list1)+len(list2)) 然后再copy(list,list1) copy(list[len(list1):],list2) 2、Copy: b = make([]T, len(a)) copy(b, a) 或者: b = append([]T(nil), a...) 3、Cut: a = append(a[:i], a[j:]...) 4、Delete: a = append(a[:i], a[i+1:]...) 或者: a = a[:i+copy(a[i:...阅读全文

博文 2015-11-09 11:00:42 fyxichen

Go语言的gob简单使用

编码结构体: package main import ( "encoding/gob" "fmt" "os" ) func main() { info := map[string]string{ "name": "xichen", "age": "24", } name := "test.gob" File, _ := os.OpenFile(name, os.O_RDWR|os.O_CREATE, 0777) defer File.Close() enc := gob.NewEncoder(File) if err := enc.Encode(info); err != nil { fmt.Println(err) } } 解码结构体: package main import ( "enc...阅读全文

博文 2015-10-23 19:00:36 fyxichen

golang 使用sql语句操作数据库的方法

func (m xxxstruct) insertxxx() (bool,string){ sqlquery :="insert into tables_name(id,name,email,content,) values(%v,%v,%v,%v)" sqlquery =fmt.Sprintf(sqlquery,m.id,m.name,m.email,m.content) ok,desc :=queryexecmysql(sqlquery) if ok { return false,desc } return true,"ok"}func queryexecmysql(parmam string)(bool string){ o :=orm.NewOrm() o.Using("deaful...阅读全文

博文 2017-02-09 17:32:12 gdutccd

Golang -- 使用 Bufferd channel 实现 线程安全的 pool

从 文章 进行转载,再次感谢 概述 我们知道,Go 语言已经提供了 sync.Pool,但是做的不怎么好,所以有必要来实现一个 Pool 代码 type Pool struct { pool chan *Client } // Create a new Pool func NewPool(max int) *Pool{ return &Pool{ pool: make(chan *Client, max) } } // Get a Client from Pool func (p *Pool) Borrow() *Client { var cl *Client select { case cl = <-p.pool: default: c1 = newClient() } return cl...阅读全文

博文 2015-06-17 20:02:37 xiaorenwuzyh

Go语言自己实现的异步小log程序.

slog.go package slog import ( "errors" "fmt" "os" "strings" "time" ) type Logger struct { console bool warn bool info bool tformat func() string file chan string } func NewLog(level string, console bool, File *os.File, buf int) (*Logger, error) { log := &Logger{console: console, tformat: format} if File != nil { FileInfo, err := File.Stat() if err ...阅读全文

博文 2015-10-30 19:00:00 fyxichen

How to serialize/unserialize type struct in GO

How to serialize/unserialize type struct in GO package main import ( "bytes" "encoding/gob" "fmt" "log" ) type Data struct { id int name string } func (d *Data) GobEncode() ([]byte, error) { w := new(bytes.Buffer) encoder := gob.NewEncoder(w) err := encoder.Encode(d.id) if err != nil { return nil, err } err = encoder.Encode(d.name) if err != nil { ...阅读全文

博文 2015-11-09 13:00:10 u012413865

go hot update (1)

package main import ( "net" "reflect" "fmt" "os" ) func main() { listener, _ := net.Listen("tcp", ":8000") for { c , _ := listener.Accept() go connHandler(c) } } func connHandler(c net.Conn) { defer c.Close() e := reflect.TypeOf(c).Elem() n := e.NumField() for i := 0 ; i < n; i++ { fmt.Fprintf(os.Stdout, "e.Field(%d) = %v %v\n", i, e.Field(i).Name,...阅读全文

博文 2015-10-23 23:00:11 eclipser1987

The Laws of Reflection(Go语言反射定律)

载自 The Go Blog The Laws of Reflection 6 September 2011 Introduction Reflection in computing is the ability of a program to examine its own structure, particularly through types; it's a form of metaprogramming. It's also a great source of confusion. In this article we attempt to clarify things by explaining how reflection works in Go. Each language'...阅读全文

博文 2016-01-28 03:00:02 basque

Go语言的排它锁sync.Mutex

package main import ( "fmt" "sync" "time" ) var m *sync.Mutex func main() { m = new(sync.Mutex) go lock(1) time.Sleep(time.Second) lock(2) fmt.Printf("%s\n", "exit!") } func lock(i int){ println(i, "lock start") m.Lock() println(i, "lock") time.Sleep(10 * time.Second) m.Unlock() println(i, "unlock") } 版权声明:本文为博主原创文章,未经博主允许不得转载...阅读全文

博文 2015-11-05 15:00:52 wkyb608

idea配置golang

用os x的installer pkg安装go到默认路径/usr/local/go下面之后,进入idea提示找不到GOROOT和GOPATH 直接在terminal输入下列命令 launchctl setenv GOROOT /usr/local/go launchctl setenv GOPATH /Users/yourname/go 再进入即可。 版权声明:本文为博主原创文章,未经博主允许不得转载...阅读全文

博文 2015-10-22 19:00:01 kcccss111

go 数组

package main import "fmt" import "reflect" func main() { var arr[5]int fmt.Println(reflect.TypeOf(arr)) var sli[]int fmt.Println(reflect.TypeOf(sli)) arr1 := [1]int{100} arr2 := [2]int{1,2} arr3 := [...]int{1,2,3,4,5} fmt.Println(arr,"\n",arr1,"\n",arr2,"\n",arr3) fmt.Println("二维数组") arr4 := [2][3]int {{1,2,3},{4,5,6}} fmt.Println(arr4) arr5 := [2]...阅读全文

博文 2015-11-07 13:00:05 eclipser1987

Go语言入门——Hello World

Go语言入门——Hello World 1.环境准备: 点击这里去官网下载对应的安装文件,我下载的是go1.4.2.darwin-amd64-osx10.8.pkg。 打开Terminal,输入go version,如果能看的版本号,说明go的环境已经配置好了。 2.Hello World 照例,第一个程序依旧是Hello World,在工作目录下新建test.go文件: package main import "fmt" // this is a comment func main(){ fmt.Println("Hello World") } 保存后,执行go run test.go,可以看到已经输出了Hello World。 2015年05月21日 $(function () { $(...阅读全文

博文 2015-09-20 17:00:00 JiezhiG

windows下生成go的全平台交叉版本

allplatform.cmd :: darwin/386 darwin/amd64 freebsd/386 freebsd/amd64 freebsd/arm linux/386 linux/amd64 linux/arm windows/386 windows/amd64 call compileplatform.cmd darwin 386 call compileplatform.cmd darwin amd64 call compileplatform.cmd freebsd 386 call compileplatform.cmd freebsd amd64 call compileplatform.cmd freebsd arm call compileplatform.cmd...阅读全文

博文 2015-10-28 20:00:06 lzf_china

GO语言之路 - 目录

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/vivian_king/article/details/80082454 目录 GO语言之路 1 - 学习准备 GO语言之路 2 - 学习计划 GO语言之路 3 - 基础 GO语言之路 4 - Hello, GO GO语言之路 5 - 语法基础 持续更新中。。...阅读全文

博文 2018-08-22 16:32:36 vivian_king

golang[43]-blockchain-serialize

真实比特币序列化参考资料:https://www.blockchain.com/btc/block/00000000000000000a1f57cd656e5522b7bac263aa33fc98c583ad68de309603本文链接: https://dreamerjonson.com/2018/12/12/golang-43-blockchain-serialize/版权声明: 本博客所有文章除特别声明外,均采用 CC BY 4.0 CN协议 许可协议。转载请注明出处...阅读全文

博文 2019-02-21 15:36:10 jonson_jackson