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

golang 获取主机网卡对应的ip

import "net" func Ips() (map[string]string, error) { ips := make(map[string]string) interfaces, err := net.Interfaces() if err != nil { return nil, err } for _, i := range interfaces { byName, err := net.InterfaceByName(i.Name) if err != nil { return nil, err } addresses, err := byName.Addrs() for _, v := range addresses { ips[byName.Name] = v.Stri...阅读全文

博文 2018-12-08 17:35:47 woahjknes

reflect.New()方法

``` package main import ( "fmt" "reflect" ) type tabler interface { TableName() string } type model struct{} func (m model) TableName() string { return "table_name" } func main() { var mod model getTableName(mod) } /* reflect.New()函数的声明 func New(typ Type) Value 根据传入的tpye,可以获取到对应的value */ func getTableName(v interface{}) { rt := reflect.TypeOf(v) rv...阅读全文

博文 2017-12-21 08:33:39 fightingforbing

golang中filepath包的使用

func Abs(path string) (string, error)package main import ( "path/filepath" "os" "fmt") func main() { // pName := os.Args[0] absName, err := filepath.Abs(pName) if err != nil{ fmt.Println(err) } fmt.Println(absName) }func Base(path string) stringpackage main import ( "path/filepath" "fmt") func main() { baseName := filepath.Base("/a/b/c/e.txt") fmt....阅读全文

博文 2018-10-11 01:35:41 ck_god

golang time

1 2 3 4 5 6 7 月日时分秒年时区标准以这个对应 const ( 43 ANSIC = "Mon Jan _2 15:04:05 2006" 44 UnixDate = "Mon Jan _2 15:04:05 MST 2006" 45 RubyDate = "Mon Jan 02 15:04:05 -0700 2006" 46 RFC822 = "02 Jan 06 15:04 MST" 47 RFC822Z = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone 48 RFC850 = "Monday, 02-Jan-06 15:04:05 MST" 49 RFC1123 = "Mon, 02 Jan 2006 15:04:0...阅读全文

博文 2014-10-04 19:26:01 ggaaooppeennngg

Go和C类型对应关系

C.char C.schar (signed char) C.uchar (unsigned char) C.short C.ushort (unsigned short) C.int C.uint (unsigned int) C.long C.ulong (unsigned long) C.longlong (long long) C.ulonglong (unsigned long long) C.float C.double. unsafe.Pointer (void*) // Go string to C string func C.CString(string) *C.char var val []byte (*C.char)(unsafe.Pointer(&val[0])...阅读全文

博文 2016-04-17 23:00:01 fyxichen

C#与golang常用数据类型对应关系

C# golang proto 范围 sbyte int8 int32 -128->127 short int16 int32 -32768->32767 int int32 int32 -2 147 483 648->2 147 483 647 long int64 int64 -9 223 372 036 854 775 808->9 223 372 036 854 775 807 byte unit8或byte int32 0->255 ushort unit16 uint32 0->65535 uint unit32 uint32 0->4 294 967 295 ulong unit64 uint64 0->18 446 744 073 709 551 615 decimal fl...阅读全文

博文 2015-09-25 03:00:00 ha666

template里的range,如何使用其他map里的index

数据类似 ```go data[users]=[]user{ {id:1,name:"test",groupid:3}, {id:2,name:"test2",groupid:2} } data[groups]=map[int]group{ 3:{id:3,name:"管理组"}, 2:{id:2,name:"普通组"} } ``` 希望输出 ```html test 管理组 test2 普通组 ``` 问题在于,在template里,循环users的时候,要把groupid转换成对应的name,怎么样转换比较高...阅读全文

Golang 随机生成ipv4和ipv6

随机生成ipv4 我们知道在计算机中每一个ipv4实际上都可以对应一个uint32的数,所以随机生成一些ip,实际上就是随机生成一些uint32的数。 type IPv4Int uint32 func (i IPv4Int) ip() net.IP { ip := make(net.IP, net.IPv6len) copy(ip, net.IPv4zero) binary.BigEndian.PutUint32(ip.To4(), uint32(i)) return ip.To16() } func RandomIpv4Int() uint32 { return rand.New(rand.NewSource(time.Now().UnixNano())).Uint32() } 随机生成i...阅读全文

博文 2019-06-27 00:32:54 L白水飘萍

interface _ golang

Interfaces are named collections of methods signatures package main import ( "fmt" "math" ) type geometry interface { area() float64 perim() float64 } type square struct { width, height float64 } type circle struct { radius float64 } func (s square) area() float64 { return s.width * s.height } func (s square) perim() float64 { return 2*s.width + 2*...阅读全文

博文 2015-03-15 10:00:01 jackkiexu

Go语言错误处理

package main import ( "fmt" "math" ) //对应错误类型输出 type ErrNegativeSqrt float64 //定义错误类型输出 func (e ErrNegativeSqrt) Error() string { return fmt.Sprintf("cannot Sqrt negative number:%f", float64(e)) } //计算平方根的方法 func Sqrt(f float64) (float64, error) { if f > 0 { return math.Sqrt(f), nil } return 0, ErrNegativeSqrt(f) } func main() { g, h := Sqrt(2) if ...阅读全文

博文 2015-06-17 23:03:18 genispan

Redis的key类型,value类型,以及value中的value类型

`Redis`有五种`value`类型,这个我也知道。

      但是我现在想知道的是,比如`key`对应的`value`是一个集合,或者是`sorted set`,那这个`set`里面可以包含的`value`的类型是有限制的吗?比如只能是`byte[]`或者`string`,还是没有限制?

      查了很久,网上的博客说来说去就是`value`五种类型。。。难道他们不知道`set`还有`list`还有`hash map`内部还会包含`value`的吗。。。这个`value`的类型却只字未提

 ...阅读全文

leetcode_1189

Golang: 思路:这个,建立个数组,对应'a','b','l','o','n',每当出现'a','b','n'时,对应数组位置加二,出现'l','o'时,对应数组位置加一,然后遍历数组,找到最小值,除以2即可 代码如下: func maxNumberOfBalloons(text string) int { res:=make([]int,5) for i:=0;i阅读全文

博文 2020-02-19 15:32:50 淳属虚构