douluchuo0801 2019-08-25 15:17
浏览 48
已采纳

插入调用方法并插入值

I have this working in another programming language, creating a url to reverse geocode a location. I am new to Go and I am slowly working building up the script.

I have the method Geofunction(x,y) with the two variables x & y From another method I call the above method and supply the values to the variables.

I just can't get it to work as expected.

Could someone point me to where to help me find the answer give me some help on this please.

I have a working coy in Python, as I learn Go I am translating scripts understand.

I have made changes to allow certain variable to accessible to the other functions. I need to understand if the method called with be able to access the variable values.

package main

import "fmt"

var Location1x, Location1y string
var Location1 string
var rev_geo string

func Geofunction(x, y) {
    var str1 string = "https://eu1.locationiq.com/v1/reverse.php?key="
    var str2 string = "**********************"  //API Key
    var str3 string = x // '48.853106'
    var str4 string = "&lon="
    var str5 string = y // '2.384202'
    var str6 string = "&format=json"
    var rev_geo string = str1 + str2 + str3 + str4 + str5 + str6

    return rev_geo
}

func Locator() {
    Location1x, Location1y = "48.853106", "2.384202"
    Location1 = Geofunction(Location1x, Location1y)
}

func main() {
    Locator()
    fmt.Println(Location1)
}```

Expected:
A string of a URL is printed.

The three errors are: 
main.go:9:18: undefined: x
main.go:9:21: undefined: y
Geofunction(Location1x, Location1y) used as value


Once I get the above sorted, I will then reuse the method to produce multiple strings in an API test that confirms specific data in the json files returned from the server
  • 写回答

2条回答 默认 最新

  • doukang7486 2019-08-25 15:24
    关注

    You are missing a few pieces.

    1. The types for x and y in the function definition are missing. Since the are both strings, the type can be defined simultaneously.
    2. The function GeoFunction is missing a return type, which should be string

    Go is quite strict about syntax and structure, unlike Python - I would strongly suggest completing the the Go Tour before transpiling any code, it will make things a lot smoother for you.

    Try something like:

    package main
    
    import (
        "fmt"
    )
    
    var Location1x, Location1y string
    var Location1 string
    var rev_geo string
    
    func Geofunction(x, y string) string {
        var str1 string = "https://eu1.locationiq.com/v1/reverse.php?key="
        var str2 string = "**********************"  //API Key
        var str3 string = x // '48.853106'
        var str4 string = "&lon="
        var str5 string = y // '2.384202'
        var str6 string = "&format=json"
        var rev_geo string = str1 + str2 + str3 + str4 + str5 + str6
    
        return rev_geo
    }
    
    func Locator() {
        Location1x, Location1y = "48.853106", "2.384202"
        Location1 = Geofunction(Location1x, Location1y)
    }
    
    func main() {
        Locator()
        fmt.Println(Location1)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题