草庐IT

map函数

全部标签

dictionary - 如何尽可能均匀地随机分割 Go 中的 map ?

我有一个简短的问题。我对golang很陌生。假设我有一张这样的map:map[int]string我怎样才能将它随机分成两个映射或数组,并尽可能接近偶数?因此,例如,如果有15个项目,它将拆分为7-8。 最佳答案 例如:funcsplit(mmap[int]string)(oddsmap[int]string,evensmap[int]string){n:=1odds=make(map[int]string)evens=make(map[int]string)forkey,value:=rangem{ifn%2==0{evens[k

go - 在 golang 中如何测试 map 或 channel 未初始化

varmmap[int]intvarcchanint如何用make测试m和c是否未初​​始化 最佳答案 您可以将这些值与nil进行比较,以查看它们是否已初始化。例如:varmmap[int]intvarcchanintfmt.Println("ismuninitialized:",m==nil)//truefmt.Println("iscuninitialized:",c==nil)//truem=make(map[int]int)c=make(chanint)fmt.Println("ismuninitialized:",m==n

arrays - Go:如何在函数中获取 slice 的长度?

我有一个函数,我想向其提供不同类型的slice,之后我想遍历它们并打印它们的内容。以下代码有效:funcplot(datainterface{}){fmt.Println(data)//fmt.Println(len(data))}funcmain(){l:=[]int{1,4,3}plot(l)}但是,当我取消注释打印slice长度的那一行时,我收到一条错误消息:invalidargumentdata(typeinterface{})forlen。知道如何获取slice的长度以便循环遍历它吗? 最佳答案 您应该尽可能避免使用int

function - 如何使用 interface{} 作为灵活的函数参数和返回类型?

我是Go的初学者,我现在正在编写一个可以调用API的函数。该函数接收一部分url(/user、/account等)和将返回的json转换为的结构(结构User或Account作为参数。所以我现在有这个:func(self*RestClient)request(actionstring,return_typeinterface{})interface{}{res,_:=goreq.Request{Uri:self.url+action}.Do()varitemreturn_typeres.Body.FromJsonTo(&item)returnitem}我尝试使用(Index是返回类型的

go - 为什么即使有锁,GO 也会出现 'concurrent map writes' panic ?

当尝试将此结构与多个goroutine一起使用时,有时我会遇到以下错误之一:fatalerror:并发映射读取和映射写入或并发映射写入看完thisthread我确保在构造函数中返回一个引用,并将一个引用传递给接收者。使用它的完整代码在thisgithubrepo中typeconcurrentStoragestruct{sync.Mutexdomainstringurlsmap[url.URL]bool}funcnewConcurrentStorage(dstring)*concurrentStorage{return&concurrentStorage{domain:d,urls:ma

go - 如何在 go 中模拟函数?

packagemainimport("net/http""sync""time")typeSessionInterface1interface{doLoginAndReadDestinations1()bool}typeSession1struct{sessionCookiestringmuxsync.MutexsessionTimetime.TimetargetAddressstringcurrentJwtstringtransport*http.Transport}varcurrentSession1Session1funcmain(){currentSession1.verify

go - serveHTTP 实用程序从哪里来的所谓的裸函数

我有这个工具:typeHandlerstruct{}func(hHandler)Mount(router*mux.Router,vPeopleInjection){router.HandleFunc("/api/v1/people",h.makeGetMany(v)).Methods("GET")}上面调用这个:func(hHandler)makeGetMany(vPeopleInjection)http.HandlerFunc{typeRespBodystruct{}typeReqBodystruct{Handlestring}returntc.ExtractType(tc.Type

go - 函数在返回语句时不退出

我有一个奇怪的问题。我在玩围棋时发现了一些我无法理解的非常奇怪的行为。当我运行findMatchingSum函数时,它搜索预期的总和,如果总和更大,我将最后一个索引减1,如果更大,则将第一个索引递增一个。然而,当我调试代码时,它首先命中if语句并且应该返回true,但是它直接运行并运行最后一个elseif语句。困惑从这里开始。在第3次迭代中,它遇到了进入该block的if语句,但没有退出该函数。这是代码;packagemainimport"fmt"vararr=[]int{1,2,4,4}funcmain(){s:=findMatchingSum(arr,8,len(arr)-1,0)

go - 使用该结构的内部函数检查映射中是否存在值

我有这个结构typeZonesmap[uint64]Zone我想有一种方法可以在该映射中找到一个值,就像这样。func(z*Zones)findById(iduint64)(Zone,error){ifzone,ok:=z[id];ok{returnzone,nil}else{returnzone{},errors.New(fmt.Sprintf("Zone%dnotfound",id))}}但是在这一行中:ifzone,ok:=z[id];ok{我收到这个错误:Assignmentcountmismatch:2=1.有很多链接表明我们可以使用该行检查map中是否存在某个值,我不知道发

for-loop - 更新 map 中的键,同时遍历该 map

我想使用URL参数将key从一个名称更新为另一个名称。我有代码,但输出不正确。见下文。这是mapvardatamap[string][]string调用函数的PUT方法r.HandleFunc("/updatekey/{key}/{newkey}",handleUpdateKey).Methods("PUT")handleUpdateKey函数,它被记录下来并准确解释了它在做什么。funchandleUpdateKey(whttp.ResponseWriter,r*http.Request){params:=mux.Vars(r)k:=params["key"]//geturlpara