草庐IT

array_to_compute

全部标签

arrays - 将数组作为golang的输入

这个问题在这里已经有了答案:Readnumbersfromos.StdinintoarrayorsliceinGo(1个回答)关闭4年前。我如何将数组作为golang的输入?funcmain(){varinPut[]float32fmt.Printf("Input?")fmt.Scanf("%s",&inPut)fmt.Println(inPut)for_,value:=rangeinPut{fmt.Print(value)}}我试过上面的代码,但没有给出正确答案,我应该使用其他类型的扫描仪吗?我想接受的输入类似于[3.2-6.7742-0.9]

arrays - 无法对二维数组的列进行 slice "cannot use Sudoku[0:9][0] (type [9]int) as type []int in assignment"

我正在使用9x9二维数组的slice制作一个简单的数独游戏。我仍然刚开始使用Golang并且有一些C++经验。我不断收到错误消息“无法将数独[0:9][0](类型[9]int)用作赋值中的类型[]int”。varrow1[]int=数独[0][0:9]该行正确地获取了二维数组第一行的值并将它们放入row1slice中,但是使用varcol1[]int=Sudoku[0:9][0]会导致上面的错误消息。我能做什么?提前致谢!例如,packagemainimport"fmt"funcmain(){varSudoku[9][9]intfmt.Println(Sudoku)varrow1[]i

arrays - 为什么连接这两个字符串会在 Go 中移动组合字符串的前 3 个字符的末尾 3 个字符?

当尝试连接两个字符串时,它们会合并,但接下来的三个字符会覆盖之前连接的文本,然后再次按预期继续连接。我怀疑这与retrieve_mapped_value(cmd.Interaction.Replies[p_index].Variable[r_index],var_swap)函数有关,因为这种现象仅发生在在build_executable的cmd.Interaction部分的双for中调用。哪里出错了funcbuild_executable(cmdshell_command,var_swapstring_matrix,is_firstbool)string{sleep_duration:

arrays - 在 Go 中使用 slice 进行子集检查

我正在寻找一种有效的方法来检查slice是否是另一个slice的子集。我可以简单地遍历它们来检查,但我觉得必须有更好的方法。例如{1,2,3}isasubsetof{1,2,3,4}{1,2,2}isNOTasubsetof{1,2,3,4}有效执行此操作的最佳方法是什么?谢谢! 最佳答案 我认为解决子集问题最常见的方法是通过map。packagemainimport"fmt"//subsetreturnstrueifthefirstarrayiscompletely//containedinthesecondarray.There

pointers - 调用结构函数给出 "cannot refer to unexported field or method"

我有这样的结构:typeMyStructstruct{Idstring}和函数:func(m*MyStruct)id(){//doingsomethingwithidhere}我还有一个这样的结构:typeMyStruct2struct{m*MyStruct}现在我有一个函数:funcfoo(str*MyStruct2){str.m.id()}但是我在编译时遇到错误:str.m.idundefined(cannotrefertounexportedfieldormethodmypackage.(*MyStruct)."".id如何正确调用这个函数? 最佳答案

syntax - 戈朗 : how to get sub element from a []interface{} alias

我为[]interface{}定义了一个别名:typestate[]interface{}如何获取状态中的子项:functest(sstate){//Howtoget1stelementins?//orHowtoconvertsbackto[]interface{}?}test([]interface{1,2,3}) 最佳答案 test([]interface{1,2,3})是错误的,应该是test(state{1,2,3}).您还可以像访问任何slice一样访问s中的第一个元素,使用s[x]:typestate[]interfac

arrays - 提高在结构 slice 中搜索值的性能

我如何优化下面的代码以搜索map数组中的特定键值(然后返回其他键值)?typeuserMapstruct{JiraUsernamestringCHProjectIDintCHIDstring}funcmain(){varuserMaps[]userMapuserMaps=append(userMaps,userMap{JiraUsername:"ted",CHProjectID:81,CHID:"23jk3f32jl3323",})fmt.Println(GetUserInfo(userMaps,"ted"))}funcGetUserInfo(userMaps[]userMap,jir

戈朗 : json Unmarshal fails to unpack simple example

关闭。这个问题是notreproducibleorwascausedbytypos.它目前不接受答案。这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这个问题的解决方式不太可能帮助future的读者。关闭6年前。Improvethisquestion我无法解决这个问题。我有一个简单的结构和另一个使用它两次的结构:typeRangestruct{Positionint`json:"position"`Lengthint`json:"length"`}typeTwoRangesstruct{FromRange`json:"from"`ToRange

arrays - GO阵列扩展

我正在测试一些go数组初始化和一般用法,但我不明白为什么不能扩展用定义长度初始化的数组?packagemainfuncmain(){arr0:=[]int{1,2,3}//add(arr0...)//OKarr1:=[3]int{1,2,3}//slice:=arr1[:]//add(slice...)//OKarr2:=[3]int{}//arr2[0]=1//arr2[1]=2//arr2[3]=3//add(arr2...)//cannotusearr2(type[3]int)astype//[]intinargumenttoadd}funcadd(terms...int)(su

去新手 : how to run goroutines shell?

我尝试运行shell-basic但没有任何反应。这是我尝试过的:Torunthisexample,downloadandinstallitwithgoget:gogetgoroutines.com/shell-basic静默完成,我看到它下载了shell基本脚本,但是当我执行shell-basic时,我得到:$shell-basic-bash:shell-basic:commandnotfound我做错了什么,还是我遗漏了什么?我感兴趣的是将go作为scripts运行.. 最佳答案 goget将获取源并将其放入您的Go路径,在she