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 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型