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

插入调用方法并插入值

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条)

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况