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

golang将interface{}转换为struct

项目中需要用到golang的队列,container/list,需要放入的元素是struct,但是因为golang中list的设计,从list中取出时的类型为interface{},所以需要想办法把interface{}转换为struct。 这里需要用到interface assertion,具体操作见下面代码: 1 package main 2 3 import ( 4 "container/list" 5 "fmt" 6 "strconv" 7 ) 8 9 type People struct { 10 Name string 11 Age int 12 } 13 14 func main() { 15 // Create a new list and put some numbers ...阅读全文

博文 2017-12-28 19:00:07 lit10050528

golang strings包方法

package main import s "strings" //strings取个别名 import "fmt" //我们给 fmt.Println 一个短名字的别名,我们随后将会经常用到。 var p = fmt.Println func main() { //这是一些 strings 中的函数例子。注意他们都是包中的函数,不是字符串对象自身的方法,这意味着我们需要考虑在调用时传递字符作为第一个参数进行传递。 p("Contains: ", s.Contains("test", "es")) p("Count: ", s.Count("test", "t")) p("HasPrefix: ", s.HasPrefix("test", "te")) p("HasSuffix: ", s....阅读全文

博文 2015-04-21 03:00:01 benlightning

golang strconv Atoi Itoa 例子

http://www.dotcoo.com/golang-strconv-atoi-itoa golang strconv atoi itoa 在做任何项目的时候都要用到字符串和数字,相互转换是最近基本的操作,哈哈 这里就介绍golang怎么做这些事情 参考代码如下: package main import ( "strconv" ) func main() { i, err := strconv.Atoi("12345") if err != nil { panic(err) } i += 3 println(i) s := strconv.Itoa(12345) s += "3" println(s) ...阅读全文

博文 2014-10-05 20:58:32 DarkWingBoy

一个CGO的问题

写了个CGO的调用,用到了crypto这个库,但是程序有时候会正常运行,有时候又会崩溃,都是在同一个地方崩溃。崩溃的情况下报了这个错误: fatal error: unexpected signal during runtime execution [signal 0xb code=0x1 addr=0xfffffffff80011c0 pc=0x3dd1c6a437] runtime stack: runtime.gothrow(0x6fd250, 0x2a) /usr/local/go/src/runtime/panic.go:503 +0x8e runtime.sigpanic() /usr/local/go/src/runtime/sigpanic_unix.g...阅读全文

golang 中的日期时间

Golang 中的日期和时间 包名 time 当前时间 time.Now() 当前时间戳 time.Now().Unix() 时间格式化string time.Now().Format("2006-01-02 15:04:05") time.Now().Format("2006-01-02") 时间戳格式化到string str_time := time.Unix(1389058332, 0).Format("2006-01-02 15:04:05") string 转化到 时间 以上是时间转化 接下来是总会用到的特殊事件 现在时间 time.Now() 今天00:00:00 now := time.Now() year, month, day := now.Date() today_str...阅读全文

博文 2015-06-17 20:04:34 csapr1987

golang将结构体转换成json,json转换成结构体

1、将结构体转换成json需要用到marshl函数(或者marshlIndent()函数)。 区别:marshl函数没有将json格式化输出,输出是一个json的字符串。----不利于直接查看json的结构。maeshldent函数 可以将json格式化输出,可阅读性提高了。 2、将json字符串转换成结构体需要用到marshl()函数。 实例如下: ```go package main import ( "log" "encoding/json" "fmt" ) type Movie struct{ Title string Year int `json:"released"` Color bool `json:"color, omitempty"` Actors []string } f...阅读全文

博文 2018-08-23 19:08:58 yuluxs

FFT Golang 实现

最近项目要用到快速傅立叶变换,自己写了个算法,测试了下,性能和精度还可以接受 len,time= 1048576 378.186167ms diff=-0.00000000000225974794 I0.00000000000936106748成功: 进程退出代码 0. 百万级别,变换花了378ms 逆变换误差和0.000000000009 l :=mvm.PowerOf2(20) arr :=make([]complex128,l) for i,_ :=range arr{ arr[i]=complex(rand.Float64(),rand.Float64()) } now :=time.Now() fft:= mvm.CoreFFT(arr,false) Println("len,ti...阅读全文

博文 2015-12-03 03:00:00 mjgb

使用redigo中mget问题

小白请教个问题哈!我使用redigo时需要用到mget这个命令批量获取参数,但是官方给的传参方式:GO('MGET','KEY1','KEY2'),但是在实际应用中使用总是参数是不定长的,我尝试传入{“key1”,“key2”}也获取不到值,请教各位大佬,有了解怎么批量传参的方式! 目前简单这么使用的:res, r_err := redis.Strings(db.Redis.Do("MGET", "test1","test2")...阅读全文

Golang_tag

Golang tag也就是go语言中的注解 比如 type PostBody struct { Ids []int `json:"Id__in__int"` }`json:"Id__in__int"` 这一段内容就是Ids这个属性的一个tag 我们可以通过反射来获取这个tag的值 如下所示 package main import ( "fmt" "reflect" ) type PostBody struct { Ids []int `json:"Id__in__int"` } func main() { // s := "{\"Id__in__int\":[101010,101009],\"text__in__string\":[\"开单\",\"测试页\"]}" // var pb st...阅读全文

博文 2015-06-17 20:03:52 u012807459

Golang(Go语言)读取文件基本用法

需要用到os和bufio包 import "os"import "bufio" 用os.Open打开一个文件,用bufio.NewReader来读取文件 file, err := os.Open("input.dat")if err!= nil { fmt.Println("failed to open") return}defer file.Close()reader := bufio.NewReader(file)for { str, err := reader.ReadString('\n') //每次读取一行 if err!= nil { break // 读完或发生错误 } fmt.Printf(str)...阅读全文

博文 2016-02-24 22:00:06 QQ245671051

在golang中如何快速找到一个interface被使用的地方?

golang中存在以下两个特性: * 可以在初始化变量时不显示的指定其具体类型 * 某个结构只要实现了某个interface规定的所有接口,那么这个结构就可以被转换为这个interface。interface的定义与结构的定义可以在不同的包中 这样就造成了对于大型工程来说,想要找到一个接口的某个方法被调用的地方就会很困难,尤其是名称还很简单的方法,比如List(xx string) error这样的。 目前想到的一个可行的办法是直接修改接口的定义,这样在编译时就可以直接把用到这个接口的地方给提示出来,但某些情况下可能还是不能定位到想要找的地方。 $(function () { $('pre.prettyprint code').each(function () { var lines = $...阅读全文

博文 2016-10-15 17:00:01 ylwh8679

golang-反射机制

1,写数据库dao层的时候用到了反射机制。在反射的时候要注意你的对象时指针还是结构体这样区别也很大。以下接受几种常用的放射方法 reflect.type of package main import ( "fmt" "reflect" ) type hehe struct { NameFile string "PrimaryKey" age int } func main() { hehe := &hehe{"ssssssssssss", 33} yingShe(hehe) } func yingShe(obj interface{}) { hehe := &hehe{"ssssssssssss", 22} for i := 0; i < reflect.TypeOf(obj).Elem()...阅读全文

博文 2016-04-10 20:00:01 shuanger_

Ethereum学习笔记3--以太坊用到的加密技术

一 加密技术总结 "crypto/ecdsa":用与数字签名 "golang.org/x/crypto/sha3" "crypto/rand" "github.com/ethereum/go-ethereum/crypto/bn256/google" "github.com/ethereum/go-ethereum/crypto/bn256/cloudflare" "crypto/elliptic" "github.com/ethereum/go-ethereum/crypto/secp256k1" "crypto/pbkdf2" "crypto/aes" "crypto/cipher" "crypto/hmac "crypto/subtle" "crypto/cipher" "crypto...阅读全文

博文 2019-08-02 21:32:38 陈兴强

golang通过oci访问oracle数据库

golang通过oci访问oracle数据库 首先需要下载 oracle driver for go that using database/sql $ go get github.com/mattn/go-oci8 然后下载Oracle Instant Client https://www.oracle.com/database/technologies/instant-client/downloads.html 因为github.com/mattn/go-oci8也是需要用到的。 解开后得到文件夹,例如:instantclient_19_3 golang访问程序 package main import ( "fmt" "log" "database/sql" _ "github.com/...阅读全文

博文 2019-06-06 13:32:38 CodingCode

选择Go语言

一直在苦恼 接下来学习那种语言。 后来找到了Google 的Go。 今天有机会把基本的语法了解了一下。 感觉不错。 一直是从事Web相关开发,用到的都是解析型语言。正好能补充我现在所需要的。 编译型语言,执行速度比较快。自动垃圾收集简洁的语法风格 很多地方很Python相似 在设计到性能方面的需求时,可以替代php等解析型语言。没有c/c++那么繁琐...阅读全文

博文 2015-06-17 23:00:47 howard_2010

Golang for range 取出数据时数据重复

由于保存数据时用json.Marshal(string)处理后保存,取出后需要进行json.Unmarshal([]byte,&str) var message MsgPacket for _, value := range chats { err = json.Unmarshal([]byte(value.Msg),&message) if err != nil { logs.Error("GetUsersPrivateChats","err = ",err) } message.Data["send_time"] = value.SendTime msg = append(msg,message) } 因为用到了&message所以数据都保存在了地址里面,每遍历一次更新一次内容到地址, ...阅读全文

博文 2019-06-15 15:02:39 贝塔船长