草庐IT

debugging - IF 语句下面的代码永远不会执行

我的代码中有一段出现了意外行为。...fmt.Println("Error:",err)iferr==nil{returnerr}fmt.Println("Donecategory")...上面的部分有以下输出Error:下面的if语句永远不会执行。如果我删除if语句,代码将按预期运行。引用:https://github.com/skarllot/flogviewer/blob/master/wlog/parser.go#L138 最佳答案 让我们逐步了解它。fmt.Println("Error:",err)如果输出是Error:.

go - Go 的 If 语句

我正在玩围棋。我想做到这一点,所以当有人输入“hi”时,它会打印出hiii这是我的代码packagemainimport("fmt""bufio""os")funcmain(){reader:=bufio.NewReader(os.Stdin)fmt.Println("SimpleShell")fmt.Println("---------------------")for{fmt.Print("->")text,_:=reader.ReadString('\n')if(text=="hi"){fmt.Println("hiii")}}} 最佳答案

go - main func 上的 reviced channel error 但是 goroutine 中的 if reviced progrenn 没有错误

我通过goroutine将数据发送到channel。当我想在主函数中接收它时,在channel的最后一次接收时出现死锁,packagemainimport("time""fmt")funcsender(chchanstring){ch输出:printresult%schenlprintresult%szhangsprintresult%slisifatalerror:allgoroutinesareasleep-deadlock!goroutine1[chanreceive]:main.main()但是,如果我也在goroutine中替换接收到的进度,则没有错误orrced。有人可以帮

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

go - "Cannot use myType as type interface{}"?我以为在 Go 中所有类型都算作 interface{}?

这个问题在这里已经有了答案:Convert[]stringto[]interface{}[duplicate](3个答案)Convertingsliceofstructstosliceofemptyinterface[duplicate](1个回答)Whycan'tIsubstituteasliceofonetypeforanotherinGo?(3个答案)Whycan'tIpassa`func()[]int`as`func()[]interface{}`ingo?(2个答案)Whyaslice[]structdoesn'tbehavesameas[]builtin?(3个答案)关闭4

go - 传播时 "Cannot use variable of type []struct as []interface"

这个问题在这里已经有了答案:sliceofstruct!=sliceofinterfaceitimplements?(6个答案)关闭8个月前。原型(prototype)函数functest(i...interface{}){//Codehere}预期用途typefoostruct{//Fields}foos:=[]foo{//foo1,foo2...}test(foos...)//ERRORtest(foos[1],foos[2],...)//OK错误cannotusefoos(variableoftype[]foos)as[]interface{}valueinargumenttot

if-statement - golang 中的 if else 语句

谁能帮我调试这个程序,每个输入只处理else部分。这是一个给学生评分的程序。学生输入分数并显示成绩funcmain(){varxintfmt.Println("Enteryourmarks")fmt.Scanf("%d",&x)if(100 最佳答案 你的逻辑有问题。改变if(100到if75因为数字不能大于100和小于75。当然其他线路也是一样。请注意,您可以减少比较。假设你一开始测试这个数字是否小于100,那么你测试完小于75就不用再测试它是否小于75了。典型的Go代码可能会在这里有一个switch而不是所有那些if/else。

go - 未能使测试套件通过 golang "sayHello() used as value"

我试图让这个测试套件通过命令提示符(hello-world_test.go):packagehelloworldimport"testing"funcTestHelloWorld(t*testing.T){output:=sayHello()if"Hello,World!"!=output{t.Fatalf("output:%s\nexpected:Hello,World!",output)}}我的代码如下(helloworld.go):packagehelloworldimport"fmt"funcsayHello(){fmt.Println("Hello,World!")}通过命令

go - 在golang的If-else block 中为变量分配不同的结构

我想做这样的事情typeStruct1{str1string}typeStruct2{int1int}ifsomething{someVar:=Struct1{str1:''}}else{someVar:=Struct2{int1:1}}somefunc(someVar)我知道我不能在一个block内声明c然后在外部访问它。我试过这样的东西typeStruct1{str1string}typeStruct2{int1int}someVar:=Struct2{b:1}ifsomething{someVar:=Struct1{a:''}}somefunc(c)它给出了一个错误-Cannot

variables - if/else 条件定义变量。未定义 : dat (variable)

我知道这是一个基本问题,但我很好奇为什么下面的代码不起作用。没有不声明此变量的用例。if(bundled=="true"){dat,err:=Asset("index.html")}else{dat,err:=ioutil.ReadFile("./index.html")}if(err!=nil){os.Exit(0)}t,_=t.Parse(string(dat))p:=Person{Scope:""}t.Execute(w,p)我得到了错误.\run.go:262:undefined:dat我确定这只是我仍在学习的基本GOLANG知识。感谢您的支持 最佳