插入调用方法并插入值

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

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

Your method definition is wrong overe here, given go is a statically typed language your function definitions should have the type of the parameters which it takes alongside the parameter names

package main

import "fmt"

var Location1x, Location1y string
var Location1 string
var rev_geo string

func Geofunction(x string, y 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)
}

This should basically work, but a better approach to this would be to use a string builder instead of appending strings. That’s more efficient