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

Golang(Go语言)内置函数之copy用法

该函数主要是切片(slice)的拷贝,不支持数组 将第二个slice里的元素拷贝到第一个slice里,拷贝的长度为两个slice中长度较小的长度值 示例: s := []int{1,2,3}fmt.Println(s) //[1 2 3]copy(s,[]int{4,5,6,7,8,9})fmt.Println(s) //[4 5 6] 有一种特殊用法,将字符串当成[]byte类型的slice bytes := []byte("hello world")copy(bytes,"ha ha"...阅读全文

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

Golang引用类型

引用类型包含 slice、map、channel 内置函数:new 为其分配零值内存 并返回指针 make 为其分配内存初始化结构 并返回对象 package main import ( "fmt" ) func main() { a := make([]int, 3) a[1] = 10 b := new([]int) b[1] = 20 //error : invalid operation: b[1] (type *[]int does not support indexing) ...阅读全文

博文 2016-09-12 12:00:34 lt695981642

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

Go语言开发学习教程

Go语言开发学习教程 Go语言开发学习教程目录如下: Go语言开发(一)、Go语言简介http://blog.51cto.com/9291927/2126775Go语言开发(二)、Go语言基础http://blog.51cto.com/9291927/2127825Go语言开发(三)、Go语言内置容器http://blog.51cto.com/9291927/2129969Go语言开发(四)、Go语言面向对象http://blog.51cto.com/9291927/2130132Go语言开发(五)、Go语言面向接口http://blog.51cto.com/9291927/2130244Go语言开发(六)、Go语言闭包http://blog.51cto.com/9291927/213030...阅读全文

博文 2018-07-08 11:35:14 天山老妖S

golang md5值计算

golang内置了md5的算法,这里只是封装一层,方便使用 func MD5Bytes(s []byte) string { ret := md5.Sum(s) return hex.EncodeToString(ret[:]) } //计算字符串MD5值 func MD5(s string) string { return MD5Bytes([]byte(s)) } //计算文件MD5值 func MD5File(file string) (string, error) { data, err := ioutil.ReadFile(file) if err != nil { return "", err } return MD5Bytes(data), nil } 查看更多: https:...阅读全文

博文 2019-04-17 03:34:41 小风吹的我乱了

Go by Example: Slices

切片是Go语言的关键类型之一,它提供了比数组更加强大的队列相关接口。 package main import "fmt" func main() { // 和数组不同的是,切片的类型仅由它所包含的元素决定。 // 使用内置函数make可以创建一个长度不为零的切片。 // 下面创建了一个长度为3,存储字符串的切片, // 切片元素默认为零值,对于字符串就是""。 s := make([]string, 3) fmt.Println("emp:", s) // 和数组一样可以使用index来设置元素值或获取元素值 s[0] = "a" s[1] = "b" s[2] = "c" fmt.Println("set:", s) fmt.Println("get:",...阅读全文

博文 2014-12-06 14:00:01 codemanship

golang -- rpc

内置的gob rpcserver package main import ( "errors" "fmt" "net" "net/rpc" "os" ) type Args struct { A, B int } type Quotient struct { Quo, Rem int } type Arith int func (t *Arith) Multiply(args *Args, reply *int) error { *reply = args.A * args.B return nil } func (t *Arith) Divide(args *Args, quo *Quotient) error { if args.B == 0 { return errors.New("d...阅读全文

博文 2014-10-04 19:26:55 flyking

关于Go内置的http 包的Post数据的问题

postform 这个玩意里面有个url.value 这个,他里面的东西只支持String,怎么办?我要放一个int进去 他给我报了个cannot use 25423901 (type int) as type string in array or slice literal 代码如下 respDomainRecord,err := http.PostForm("http://dnsapi.cn/Record.List",url.Values{"login_email":{login_email},"login_password":{login_password},"format":{"json"},"domain_id":{25423901}}) if err !=nil{ ...阅读全文

go语言结构体反射的使用

reflect.Value区分CanSet和Can not Set的, 所以, 必须要返回成Can set的reflect.Value如: s := reflect.ValueOf(&t).Elem() 然后就可以happy的设值了, 可是不能随便设值的, 一个通用的方法就是使用Set(v Value)方法, 所以之前必须将值转成reflect.Value类型, 下面的这段代码就是转成Value类型 sliceValue := reflect.ValueOf([]int{1, 2, 3}) // 这里将slice转成reflect.Value类型 完整参考: type T struct { Age int Name string Children []int } t := T{12, "so...阅读全文

博文 2015-11-14 18:00:00 haitgo

如何判断zookeeper cluster是否可用

如何判断zookeeper cluster是否可用 还没有找到更好的办法,这个办法很土,就是客户端发起命令一个list命令,看看能不能正确得到结果;list的目标可以是根路径(/),或者其他内置的路径,例如(/zookeeper)。 下面是golang的例子,使用了github.com/samuel/go-zookeeper/zk库文件。 package main import ( "log" "time" "github.com/samuel/go-zookeeper/zk" ) var ZOOKEEPER_SERVERS = []string { "zookeeper1.example.com:2181", "zookeeper2.example.com:2181", "zookeepe...阅读全文

博文 2019-04-01 18:34:42 CodingCode

golang error 包 使用 以及error类型原理

package main import ( "errors" "fmt" ) func f1(code int) (int, error) { if code == 1 { return -1, errors.New("msg test error") } return code, nil } type MsgError struct { Code int Msg string } func f2(code int) (int, error) { if code == 1 { return -1, &MsgError{code, "struct msg test error"} } return code, nil } //golang 内置error 为interface类型 /* 定义方...阅读全文

博文 2016-10-27 02:00:03 liangguangchuan

go语言 类型:基础类型和复合类型

Go 语言中包括以下内置基础类型:布尔型:bool整型:int int64 int32 int16 int8 uint8(byte) uint16 uint32 uint64 uint浮点型:float32 float64复数型:complex64 complex128字符串:string字符型:rune错误型:error Go 语言中包括以下内置复合类型: 数组:array切片:slice指针:pointer字典:map通道:chan结构体:struct接口:interfac...阅读全文

博文 2015-10-09 11:00:11 osfipin

Go语言基础:struct

跟C语言或其它语言一样,也有结构体struct。C语言中用关键词typedef来给结构体定义,Go中用的都是type。 struct语法格式 type typeName struct { ... } 例如: type person struct { name string age int } struct声明 var P person P.name = "Sun" P.age = 30 //也可以 P := peron{"Sun", 30} //通过 field:value 的方式初始化,这样可以任意顺序 P := person{age:29, name:"Sun"} 上面有三种声明的方法,其中第三种比较特殊,是Go独有的声明。 struct的匿名字段 匿名字段也称为嵌入字段,C语言中也有这...阅读全文

博文 2016-09-16 06:00:00 uudou

类型和类

golang里常规的类定义一般为如下形式: type $name struct{ property01 int property02 int } func (t * name) tfunc() {} 这里一直有个 误解:struct充当了其他语言中的class关键字 其实在Golang里,类型就是类,所以我们说是类型的某个方法,类型实现了某个接口。 以上定义应当解读为 property01是struct类型(别名name)的一个属性,tfunc是struct类型(别名name)的一个方法 实际上method的定义可以依赖于所有的自定义类型。所谓自定义类型,就是通过type语句给一些内置类型起了个"别名"后所定义的新类型。 type Sex string func (s *Sex) chang...阅读全文

博文 2019-03-04 17:34:45 Linrundong

元素查找包slicelement

一个 slicelement 包,用于查找一个元素,是否在列表中存在。支持 int、string、float 内置类型,也支持 struct 类型。比如:查找一个指定字段的元素值,是否在 []struct 中存在 我们经常遇到 append(data, element) 往列表中添加元素,如果 data 不存在该元素,则添加。还有一种情况也经常遇到的是,一个 []struct 数据复杂类型,判断 struct 中某个字段值是否存在,不存在则添加。 该 slicelement 包,支持这种查找,方便和节约了大家的宝贵时间 形如:  slicelement.Contains([]string{"Joe", "David", "Bruce Lee&#...阅读全文

Go语言实践技巧(6)——map key的选择

The map key can be a value from any built-in or struct type as long as the value can be used in an expression with the == operator. Slices, functions, and struct types that contain slices can’t be used as a map key. map key可以使用任何内置类型或结构类型的值,只要这个值可以使用在==表达式中。slice,函数,和包含slice的结构体不能用作key。 参考资料: Go in Action...阅读全文

博文 2017-06-24 19:19:52 肖楠

Golang引用类型

引用类型包含 slice、map、channel 内置函数:new 为其分配零值内存 并返回指针 make 为其分配内存初始化结构 并返回对象 package main import ( "fmt" ) func main() { a := make([]int, 3) a[1] = 10 b := new([]int) b[1] = 20 //error : invalid operation: b[1] (type *[]int does not support indexing) ...阅读全文

博文 2016-09-07 07:00:03 lt695981642

go的切片实现stack问题

日常刷题刷到一个要用到栈的问题,go没有内置stack就用切片实现了,然而效率低的过分,不知道是不是我不会用导致的,发给大家看看。 这是golang用时和代码: ![image.png](https://static.studygolang.com/180429/2eb27ace8260cab2f024385da75d63e9.png) 这是C艹用时: ![image.png](https://static.studygolang.com/180429/bb76f0eab74c890b09bb65433d04c679.png) C艹代码:


class Solution {
public:
    vector dailyTe...阅读全文

Go语言编程(十一)之类型系统

类型系统 type system 类型系统是指一个语言的类型体系结构。一个典型的类型系统通常包含如下基本内容: 基础类型,如byte、int、bool、float等 复合类型,如数组、结构体、指针等 可以指向任意对象的类型(Any类型) 值语义和引用语义 面向对象,即所有具备面向对象特征(比如成员方法)的类型 接口 类型系统描述的是这些内容在一个语言中如何被关联。 为类型添加方法 在Go语言中,你可以给任意类型(包括内置类型,但不包括指针类型)添加相应的方法,例如 type Integer int func (a Integer) Less(b Integer) bool { return a < b } 在这个例子中,我们定义了一个新类型Integer,它和int没有本质不同,只是它为内置...阅读全文

博文 2016-10-17 15:00:05 JoeySheng

【golang】 关于对象,接口讲解

GO 对于 “对象”的解释 一:在Go语言中,你可以给任意类型 ,添加相应的方法, type Integer int func (a Integer) Less(b Integer) bool { return a < b } 在这个例子中,我们定义了一个新类型Integer,它和int没有本质不同,只是它为内置的 int类型增加了个新方法Less()。 这样实现了Integer后,就可以让整型像一个普通的类一样使用: func main() { var a Integer = 1 if a.Less(2) { fmt.Println(a, "Less 2") } } GO 对于 “接口”继承的解释 二:在Go语言中,只需要实现了接口要求的所有函数,我们就说实现了该接口。 type IFil...阅读全文

博文 2020-01-17 17:32:40 阿阿阿黄

Go pkg学与练 - buildin

builtin package 包含go语言中的各种类型:

unint8, uint16, uint32, uint64 int8, int16, int32, int64 float32, float64, complex64, complex128 string, int, uintptr, byte, rune const iota = 0, nil type error interface {} 
以及常用的内置函数: ``` func append(slice []Type, elems ...Type) []Type func copy(dst, src []Type) int func delete(m map[Type]Type...阅读全文

博文 2018-03-29 14:11:56 Stanley

[leetcode in golang]118、杨辉三角

补充知识:讲解了golang内置make函数的三个参数https://blog.csdn.net/weiyuefei/article/details/77968699 func generate(numRows int) [][]int { //res:=[][]int{[]int{1},[][]int{1,1}} 这个地方的错误 //beg:=[]int{1} 这个地方的错误 beg数组是每一行 res:= [][]int{[]int{1}, []int{1,1}} for i:=2;i阅读全文

博文 2019-09-26 01:32:42 aside section ._1OhGeD

panic+recover golang 中的try catch

一、知识点、 1、用到go 关键字定位 2、用到defer 延后处理最终执行 3、panic,相当于try 4、recover ,相当于catch 5、内置函数说明: image.png 二、代码 、、、 package main import ( "fmt" "time" ) func serve(ch <-chan int) { for val := range ch { go handle(val) } } func handle(x int) { defer func() { if err := recover(); err != nil { fmt.Println("work failed at :", err) } }() if x == 4 { panic(x) } fmt.P...阅读全文

博文 2018-09-07 18:34:48 DamonYi

Go 中 recover 与 panic 区别

概念panic 与 recover 是 Go 的两个内置函数,这两个内置函数用于处理 Go 运行时的错误。 panic用于主动抛出错误, recover 用来捕获panic 抛出的错误。 func main() { //捕获 异常 defer func() { if p := recover(); p != nil { fmt.Printf("panic recover! p: %v", p) //类型判断 str, ok := p.(string) if ok { err := errors.New(str) fmt.Println(err) } else { err := errors.New("panic") fmt.Println(err) } } }() fmt.Println("...阅读全文

博文 2019-11-20 21:02:42 风云

Golang学习笔记-接口和错误

接口 Go接口定义了方法后,其它类型只要实现了这些方法就是实现了接口。 type Person interface { speak() } type Student struct { } type Worker struct { } func (student Student) speak(){ println("I am student") } func (worker Worker) speak(){ println("I am worker") } func main() { var person Person person=Worker{} person.speak()//I am worker person=Student{} person.speak()//I am studen...阅读全文

博文 2019-09-27 23:32:44 aside section ._1OhGeD

go map 学习

什么是map map 是在go 中将值(value) 与 键(key) 关联的内置类型,通过相应的键可以获取到值定义类型为 map[key]value 一、 创建map ``` package mainimport "fmt"func maptest() {// 1、声明方式1 mapmap2 :=map[int] string{1:"hello",2:"world"}fmt.Println(map2) // 输出map2 :=map[int] string{1:"hello",2:"world"}fmt.Println(map2) //2、声明方式2 声明一个空map map2 :=map[int] string{} fmt.Println(map2) // 输出 map[] // 3、 ...阅读全文

博文 2020-01-07 23:33:14 水滴石川1