草庐IT

type-aliases-package

全部标签

types - Go 中的别名类型仅在未命名时才可分配?

在下面的代码片段中,最后三个赋值产生编译错误:packagemaintype(Foo[]float64Baz[2]float64Mehmap[string]stringFaqchanintTetfunc()Hueinterface{}TaiboolFozstringBarfloat64)funcmain(){var(fooFoo=[]float64{1,2,3}_[]float64=foobazBaz=[...]float64{1,2}_[2]float64=bazmehMeh=make(map[string]string)_map[string]string=mehfaqFaq=ma

types - golang 在 switch 中动态创建接收器

我想使用Process接口(interface)中实现的Read和Write方法从不同来源读取、提取和保存数据该代码在第一个example中正常工作:typeProcessinterface{Read()write()string}typeNcstruct{datastring}typeCtdNctypeBtlNcfunc(nc*Ctd)Read(){nc.data="CTD"}func(nc*Ctd)Write()string{returnnc.data}func(nc*Btl)Read(){nc.data="BTL"}func(nc*Btl)Write()string{return

reflection - 戈朗 : How can I use refect package with exsisting library

我想从函数名调用现有库中的函数。在golang中,只要从methodname调用method就OK了,因为reflectpackage有(vValue)MethodByName(namestring)。但是,对于调用方法,所有方法参数都应该是reflect.Value。如何调用参数不是reflect.Value的函数。packagemain//-------------------------------//Exampleofexistinglibrary//-------------------------------typeClientstruct{idstring}typeMet

go - 编写 CLI : How to avoid showing the typed password on screen

这个问题在这里已经有了答案:getpasswdfunctionalityinGo?(11个答案)关闭7年前。我正在使用Go编写命令行工具,其中一个命令将查询用户的用户名和密码(并将其保存到主目录内的配置文件中)。目前我还不知道如何将键入的密码替换为“*”,甚至无法像许多命令行工具那样不键入任何内容。在使用golang时如何做到这一点?

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是不同的类型(尽管它们

戈朗 : Zip Package Modified Time Defaults to 30 November 1979, 00:00

如何将zip文件的修改时间更改为time.Now()?当我创建一个zip文件时,我得到的修改时间是1979年11月30日00:00。那么有没有简单的方法可以将修改时间设置为现在呢? 最佳答案 我发现这个函数可以满足我的需求:funcFileInfoHeader(fios.FileInfo)(*FileHeader,error)。这将获取一个输入数据文件并创建一个部分填充的FileHeader。 关于戈朗:ZipPackageModifiedTimeDefaultsto30November

戈朗 : 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{

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

git - Dep xxx 已恢复,但无法加载,错误为 : Package xxx not found

我在my-app中有一个golang应用程序(克隆的gitrepo)。文件夹myapp与我的Dockerfile位于同一位置。我将我的项目复制到其中。这是我的dockerfile:FROMgolang:1.7RUNgogetgithub.com/tools/godepADDpriv/.netrc/root/.netrcWORKDIR/go/src/my-appADD./my-app.RUNgodeprestore-v现在godeprestore-v失败了。很多repo都恢复得很好,有些被跳过了:godep:Restoringdependency..godep:Restoringdepe