douzhanyan5015 2018-03-17 18:39
浏览 48

在Go语言中,函数结尾缺少返回

package main
import (
       "fmt"
)


func iLoveGoLang(sign string) (int , int) {
  if sign == "!" {
    return (14 - 2),( 3 + 3 - 6);
} else if sign == "@" {
    return (41 - (20 * 2)),(5 - 4)
}  else if sign == "$" {
    return 1,3
} else if sign == "^" {
    return 2,2
}  else if sign == "5" {
    return 3, 2
} else if sign == "(" {
    return (4 * 2) - 1, 1 
} else if sign == ")" {
    return (2*2), 2
} else if sign == "d" {
    return 2, 5
} else if sign == ">" {
    return (3 * 3) + 2, 1
} else if sign == "~" {
    return (2 * 2), (3 * 1)
} else if sign == "#" {
    return 2,1
} else if sign == "+" {
    return 13,1
} else if sign == "&" {
    return (2+3),1
} else if sign == "/" {
    return (3 + 4), 2
} else if sign == ";" {
    return (33 / 11), 3
} else if sign == "e" {
    return (2 + 3), (8 - 5)
}


    return -1,1
}

 func getLetter(value int) string {



 } 


  func main() {
  var inputString = "^(>));@&^$^d5(5>/()!ed(e5>/&e&!^"

  var gdg, srilanka = iLoveGoLang("@")
  var letter = getLetter(gdg * srilanka)

//you need to do that for all letters.

 }

I am unable to get an output. Though I added a return type to the getLetter function it does not work. It also keeps saying imported and not used: "fmt".

  • 写回答

1条回答 默认 最新

  • doujingke4981 2018-03-17 18:50
    关注

    If you define a function with a return value you also need to return something:

    func getLetter(value int) string {
        return ""
    } 
    

    This will fix your first error.

    For the second error just remove the import ( "fmt" ) part. Go does not allow you to import a package and then not use it.

    EDIT: as is being suggested: run go imports on the file will automatically add/remove imports. There are editors out there that do this automatically on every save for you. I prefer Goland but also VSCode has a great Go-Plugin. There are other great editors that support Go of course.

    Same with variables: inputString and letter are declared but not used. If you are still in programming phase but want to run it already you can either print the variables:

    fmt.Println(inputString, letter)
    

    or assign them to the empty receiver:

    _ = inputString
    

    Then the compiler considers them used.

    Here your code in a runnable fashion: playground

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号