草庐IT

structural-typing

全部标签

go - 关于 "cannot use time.Now() (type time.Time) as type "

使用以下类型定义获取“不能使用time.Now()(类型time.Time)作为字段值中的类型typetime”import("time")typetypetimetime.TimetypeFriendsstruct{NamestringBirthdaytypetime}John:=Friends{Name:"John",Birthday:time.Now()}如果我用直接类型形式(time.Time)替换typetime,就没有问题。GO的规则背后是什么??:> 最佳答案 time.Time和typetime是不同的类型(尽管它们

go - 如何在Workiva/go-data structures/中使用b tree/plus

我一直需要二叉树的实现。我在这里找到了一个。https://github.com/Workiva/go-datastructures/tree/master/btree/plus但我不太确定如何使用它。这个repo中的其他数据结构非常简单。只需调用他的包并运行这些方法。但是这个btree有点令人困惑我只是想要一个简单的示例,说明如何创建、插入和检索由此包创建的树。创建btree/plus插入key检索范围 最佳答案 godoc因为它说“在撰写本文时,树还没有完全完成”,而且它似乎根本没有公开用于创建树的公共(public)接口(in

performance - 戈朗 : right way to store map structure in lru cache

我有一个像这样的结构:map[key]value,我想通过一个字符串将它存储在"github.com/golang/groupcache/lru"中键,例如cacheKey。这是我的问题:我发现每当我想更新缓存项时,我都需要先获取:item:=cache.Get(cacheKey)ifv,ok:=item[key];ok{item[key]=new_valuecache.Add(cacheKey,item)}这样做是否正确?或者,正如一些人所建议的,我需要重新设计我的结构,以确保我可以在任何时候更新它时执行cache.Add(cacheKey,item)。或者,我什至应该使用像cach

戈朗 : building interfaces for existing types to make code testable

为了使调用sql包的代码可测试,我构建了以下2个接口(interface):typeDatabaseinterface{Close()errorQuery(string,...interface{})(DatabaseRows,error)}typeDatabaseRowsinterface{Close()errorNext()boolScan(...interface{})error}我要测试的实际代码是:funcgetDatabase(connectionStringstring)(dbDatabase,errerror){ifdb,err=sql.Open("mysql",con

go - 为什么 golang duck typing 在我下面的代码中对我不起作用?

在下面的代码中,我尝试创建模型接口(interface)和API接口(interface)的具体实现:packagemainimport"fmt"/////////typeModelinterface{ID()string}typeAPIinterface{Create(Model)}/////////typeConcreteModelstruct{}func(modelConcreteModel)ID()string{return"123"}func(modelConcreteModel)Name()string{return"aron"}typeConcreteAPIstruct{

go - 带有两个外键的中间模型 : file structure?

目标是创建一个中间模型(user_product),它有两个外键:user和product。我们能否通过外部文件(以某种方式)中的用户和产品结构来实现这一点,或者我们必须将它们与UserProduct放在同一个文件中,就像在文档中一样?此时,将它们放在外部并在UserProduct中导入它们,当然会抛出导入循环错误。结构:app/models/product.gouser.gouser_product.go问题是,如果我使用import"github.com/somehow/somehow/models"在user_product中导入product.go,显然它还导入了user_pr

Golang unix 套接字 : registering same type for RPC on different sockets?

我在Go中工作,对使用unix套接字有点陌生。尝试搜索类似的问题,但找不到任何内容,如果之前已经回答过,我们深表歉意。我想用unixsockets模拟一个机器集群进行测试。我正在测试我的Raft实现,所以我想在不同的unix套接字上注册相同类型的多个对象(一个庞大的结构)。但是看我写的一个简单的例子,效果似乎不是我想要的:为同一个导出方法拨不同的套接字似乎在单个端口上崩溃:packagemainimport("net""fmt""net/rpc""log""sync")typeServerstruct{namestring}typeSpeakArgsstruct{}typeSpeakR

mysql - golang mysql DESCRIBE表导致driver.Value type nil错误

我第一次尝试使用golang查询MySQL数据库,但是当我运行命令gorunmain.go时出现以下错误。2017/10/2221:06:58sql:Scanerroroncolumnindex4:unsupportedScan,storingdriver.Valuetypeintotype*stringexitstatus1这是我的main.go主.gopackagemainimport("log""database/sql")import_"github.com/go-sql-driver/mysql"vardb*sql.DBvarerrerror//mainfunctiontob

sorting - go type conversion - 使用共享接口(interface)对 2 片不同的接口(interface)进行排序

下面的示例包含2个接口(interface)Foo和Bar,它们都实现了相同的接口(interface)Timestamper。它还包含实现sort.Interface的类型ByTimestamp.如函数main所示,我想使用类型ByTimestamp对Foo的slice和slice进行排序条形图。但是,代码将无法编译,因为它无法将foos(类型[]Foo)转换为ByTimestamp类型,并且无法将bars(类型[]Bar)转换为ByTimestamp类型。是否可以使用实现sort.Interface的单一类型对实现相同接口(interface)的2个不同接口(interface)s

ssl - HTTP 客户端错误 : asn1: structure error: base 128 integer too large

我有一个非常基本的Go应用程序,代码如下:varclient=&http.Client{Timeout:time.Duration(30*time.Second),}//skippedpayload...response,err:=client.Post(apiUrlLogin,contentType,&payload)err返回:Posthttps://xxx/api/login:tls:failedtoparsecertificatefromserver:asn1:structureerror:base128integertoolargeGo版本为goversiongo1.10.2