草庐IT

non-public-selectors

全部标签

sorting - 对具有公共(public)字段的不同结构进行排序的最佳解决方案

我有这样的结构类型typeAstruct{NamestringCreatedAttime.Time...}typeBstruct{TitlestringCreatedAttime.Time...}typeCstruct{MessagestringCreatedAttime.Time...}还有一个通用slicevarresult[]interface{}包含A、B和C元素(将来还会有更多元素)我想按“CreatedAt”对slice进行排序。什么是最好的解决方案?我想避免检查类型或转换... 最佳答案 无论如何,您可以拥有包含这两种

go - 看不到 struct Golang 的公共(public)函数

我没有看到我定义的结构的公共(public)方法。有人可以让我明白为什么吗?这是代码://DataSaver.go:packageDataStorageimport("fmt""os")typeDataSaverstruct{//doesn'trelevanttomyquestionfileNamestringfile*os.File}funcPrintStr(){fmt.Println("hello")}然后,我在其他类中有一个主要方法。我初始化了结构,我想调用PrintStr()函数。但是,我无法调用此方法。为什么?谢谢! 最佳答案

go - "non-declaration statement outside function body"错误

关闭。这个问题是notreproducibleorwascausedbytypos.它目前不接受答案。这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这个问题的解决方式不太可能帮助future的读者。关闭4年前。Improvethisquestion对于我的httpfunc方法,我不断收到函数主体外的非声明语句错误。我不确定为什么在我修复了一些全局变量后它一直出现。packagemainimport("net/http""github.com/gorilla/websocket")varaudioMessage[]bytevarwhatType

arrays - 组合公共(public)前缀子串

我有一个消息流进入Gochannel,其中大部分看起来像:T:添加包到图表:acl-devel/2.2.52_4/armv6l-muslT:将包添加到图中:rofs-filtered/1.7_1/x86_64-muslT:将包添加到图中:rofs-filtered/1.7_1/HOSTT:将包添加到图表:libshout/2.4.1_1/i686T:将包添加到图中:mate-terminal/1.18.1_1/armv6lT:将包添加到图中:bullet-devel/2.86.1_1/x86_64-muslT:将包添加到图中:bullet-devel/2.86.1_1/HOSTT:添加

go - 无法从 url 中找到公共(public)文件

我正在尝试使用ioutil.ReadFile()获取公开可用文件的内容,但找不到该文件:panic:openhttp://www.pdf995.com/samples/pdf.pdf:没有这样的文件或目录这是我的代码://Readingandwritingfilesarebasictasksneededfor//manyGoprograms.Firstwe'lllookatsomeexamplesof//readingfiles.packagemainimport("fmt""io/ioutil")//Readingfilesrequirescheckingmostcallsforer

go - 主.go :9: use of package str without selector

我在TourofGo的解释器中有以下内容:packagemainimport("golang.org/x/tour/wc"str"strings")funcWordCount(sstring)map[string]int{results:=make(map[str]int)words:=str.Fields(s)returnmap[string]int{"x":1}}//funcmain(){//wc.Test(WordCount)//}这是基于https://tour.golang.org/moretypes/23我的错误是tmp/sandbox169629521/main.go:9

go - 语法错误 : Non-declaration statement outside function body, 但所有内容都有声明

这行不通:packagemainvarformatterstring="fmt"import(formatter)funcmain(){fmt.Println(formatter)}我得到:语法错误:函数体之外的非声明语句即使一切都有声明。 最佳答案 根据Gospecification:Eachsourcefileconsistsofapackageclausedefiningthepackagetowhichitbelongs,followedbyapossiblyemptysetofimportdeclarationsthatd

在另一个文件中转到公共(public)结构

我是GO的新手,不了解一些基础知识-所以我真的不知道如何向谷歌询问。所以我有一个包含2个文件的项目,它们都在main包中——src的根目录。一个文件是main.gopackagemainvar(node*NodeDTO)funcmain(){node=&NodeDTO{ID:1}}还有一个是dto.gowithpackagemaintypeNodeDTOstruct{IDint}所以main.go告诉我-“undefined:NodeDTO”。但是如果我在main.go附近创建一个dirdto并从那里使用我的NodeDTO就像packagemainimport"another_tcp_

go - 在不同类型的结构之间复制公共(public)字段

我有两个结构体,它们的类型如下:typeUserStructstruct{UserIDstring`bson:"user_id"json:"user_id"`Addressstring`bson:"address"json:"address"`Emailstring`bson:"email"json:"email"`CreatedAttime.Time`bson:"created_at"json:"created_at"`PhoneNumberstring`bson:"phone_number"json:"phone_number"`PanCardstring`bson:"pancar

go - 在 Golang 中使用全局列表变量。接收 "Use of package list without selector"

我有一个名为rooms的全局链表。它将存储该用户输入的所有房间的名称。在我的函数创建中,我试图引用这个名为房间的列表。我在我的主要功能中实例化列表。当我尝试将项目添加到列表“房间”时,我收到错误“使用不带选择器的包列表”。我希望能够从我的创建函数中向我的名为房间的列表中添加一个字符串。packagemainimport("net""fmt""bufio""os""container/list")varroomslistfunccreate()string{reader:=bufio.NewReader(os.Stdin)fmt.Print("NametheChatroom");inpu