草庐IT

php接口

全部标签

go - Stringer接口(interface),int类型

我刚刚通过TourofGo学习golang语言,我到达了Stringer部分。(https://tour.golang.org/methods/17)我试图理解它是如何工作的,所以我尝试了它。它没有用。代码:packagemainimport("fmt")typeIintfunc(iI)String()string{returnfmt.Sprintf("%v",i)}funcmain(){i:=I(10)fmt.Println(i)}当我运行示例时出现错误,我不太明白问题是什么。我的理论是使用String()方法:当我运行打印机代码时,程序会查找stringer方法,如果存在则运行它。

php - php 和 golang 之间的 lz4 问题

我尝试在php中使用lz4_compress压缩数据并使用https://github.com/pierrec/lz4解压缩数据在戈兰但它失败了。lz4_compress输出似乎遗漏了lz4header,block数据也略有不同。请帮我解决问题。输出:DAAAAMBIZWxsbyBXb3JsZCE=packagemainimport("bytes""encoding/base64""fmt""github.com/pierrec/lz4")funcmain(){a,_:=base64.StdEncoding.DecodeString("DAAAAMBIZWxsbyBXb3JsZCE="

go - 嵌套接口(interface) : X does not implement Y (Wrong type for Z method)

在一个包中我有一个接口(interface)Repository有一个方法GetReporter返回一个接口(interface)Reporter.这是由一个函数使用Execute这需要Repository并得到它的Reporter通过GetReporter功能。在另一个包中我有一个结构GithubRepository有一个方法GetReporter返回GithubReporter.在第三个包中,我想调用Execute使用GithubRepository在包#1外运行实例。我正在尝试让包1和包2彼此独立,而不是从另一个导入某些东西。第三个包应该结合前两个。Golang返回:cannot

Go:指向类型的指针与接口(interface)不兼容

我不确定如何正确地表述问题。我有2个go代码片段,它们应该做完全相同的事情,但显然它们没有,因为一个工作,另一个不工作(不编译)funcwriteSomething(writer*io.Writer){}funcmain(){file,_:=os.Create("error.log")varlogWriterio.Writer=filewriteSomething(&logWriter)}funcmain2(){file,_:=os.Create("error.log")writeSomething(&file)}Playgroundmain()有效而main2()无效。prog.go

function - 了解 Golang 中的接口(interface)

我正在尝试理解Go中的接口(interface)。我写了这个:packagemainimport"fmt"typeAnimalstruct{NamestringAbilitystring}typeAbilityShowerinterface{ShowAbility()string}func(aAnimal)ShowAbility()string{returnfmt.Sprintf("%scan%s",a.Name,a.Ability)}funcmain(){varDogAnimal=Animal{Name:"Dog",Ability:"Walk",}Dog.ShowAbility()}

php - 使用golang解密用php openssl_encrypt加密的文件

首先。我在这里如履薄冰!我有一个从php获得的加密文件。我正在尝试用golang解密它。php应用程序使用公共(public)RSAkey来加密用于使用aes-256-cbc加密的key。我已经创建了一些概念验证代码,但我做不对。尽管key和iv在两边看起来都是正确的,但也有不正确的地方。结果只是垃圾。我怀疑某些编码不匹配(期望base64,获取字符串字节......某事)或者我误解了一些概念。加密:解密:packagemainimport("crypto/aes""crypto/cipher""crypto/rand""crypto/rsa""crypto/x509""encodin

go - 将指针变成接口(interface)后如何检查指针

如何比较转换为接口(interface)的指针?packagemainimport("fmt")typeContainerInterfaceinterface{Check(ContentInterface)bool}typeContentInterfaceinterface{BelongsTo(ContainerInterface)bool}typeBaseContainerstruct{}func(container*BaseContainer)Check(contentContentInterface)bool{returncontent.BelongsTo(container)}

go - GoLang 对由结构和嵌入式结构实现的接口(interface)的类型转换是如何工作的

我最近发现一段代码在做一些我不理解的事情。有多个结构具有相同的嵌入式结构和一个接口(interface),该接口(interface)定义返回指向每个结构的指针的方法。此接口(interface)由嵌入式结构实现,但仅“部分”由各个结构实现,因此,每个结构仅实现返回指向该结构的指针的方法。为了更好的理解,这里有代表性的代码:typeBarStocksinterface{GetVodka()*VodkaGetMartini()*MartiniGetBourbon()*BourbonGetNegroni()*NegroniGetManhattan()*Manhattan}typeBaseA

go - 如何填充作为函数引用传递的接口(interface)片段

我有一个小例子,我尝试在一个函数中填充一个[]Entry(其中Entry是一个接口(interface))slice,当参数是单个Entry时这工作正常,但是当我试图传递一个条目slice时我无法通过指针找到我的方式。packagetempimport("encoding/json"uuid"github.com/satori/go.uuid")typeBaseEntrystruct{IDuuid.UUID}func(entry*BaseEntry)GetID()uuid.UUID{returnentry.ID}typeEntryinterface{GetID()uuid.UUID}f

go - 无法设置类型为接口(interface)的结构的字段{}

我一直在与reflect包作斗争。下面的代码符合我的预期:packagemainimport("reflect""log")typeCarstruct{Modelstring}typePersonstruct{NamestringCars[]Car}funcModifyIt(parentinterface{},fieldNamestring,valinterface{}){slice:=reflect.ValueOf(parent).Elem()nth:=slice.Index(0)//row:=nth.Interface()//thislinecauseserrorsrow:=nth