dongtuo3370 2018-02-27 01:59
浏览 295
已采纳

Go程序无法返回太多参数

I have made this simple software to connect to AWS and generate a presigned put url:

package main

import (
    "fmt"
    "log"
    "time"

    "github.com/minio/minio-go"
    "github.com/kelseyhightower/envconfig"
)

// AWS contains the configuration to connect and create a presigned url on AWS.
type AWS struct {
  Endpoint        string
  AccessKeyId     string
  SecretAccessKey string
  SSL             bool
  BucketHost      string
  BucketKey       string
}

func main() {
    url, err = generatePresignedPutUrl()
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println(url)
}

func generatePresignedPutUrl() {
    var aws AWS
    if err := envconfig.Process("aws", &aws); err != nil {
        return err
    }

    svc, err := minio.New(aws.Endpoint, aws.AccessKeyId, aws.SecretAccessKey, aws.SSL)
    if err != nil {
        return err
    }

    url, err := svc.PresignedPutObject(aws.BucketHost, aws.BucketKey, time.Second * 24 * 60 * 60)
    if err != nil {
        return url, err
    }
}

But when I try to run go run main.go, the following errors show in my terminal:

# command-line-arguments
./main.go:23:5: undefined: url
./main.go:23:10: undefined: err
./main.go:23:39: generatePresignedPutUrl() used as value
./main.go:24:8: undefined: err
./main.go:25:19: undefined: err
./main.go:27:17: undefined: url
./main.go:33:9: too many arguments to return
    have (error)
    want ()
./main.go:38:9: too many arguments to return
    have (error)
    want ()
./main.go:43:9: too many arguments to return
    have (*url.URL, error)
    want ()

What exactly am I doing wrong here? I started to learn go a few days ago. I'm sorry if this is to much obvious.


Update:

I was able to figure it out that I should declare what I'm returning from generatePresignedPutUrl, but still:

package main

import (
    "fmt"
    "log"
    "time"

    "github.com/minio/minio-go"
    "github.com/kelseyhightower/envconfig"
)

// AWS contains the configuration to connect and create a pre-signed url on AWS.
type AWS struct {
  Endpoint        string
  AccessKeyId     string
  SecretAccessKey string
  SSL             bool
  BucketHost      string
  BucketKey       string
}

func main() {
    url, err := generatePresignedPutUrl()
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(url)
}

// generatePresignedPutUrl connects to S3 and generates a pre-signed put url
// using configurations from environemnt variables.
func generatePresignedPutUrl() (url string, err error) {
    var aws AWS
    if err := envconfig.Process("aws", &aws); err != nil {
        return err
    }

    svc, err := minio.New(aws.Endpoint, aws.AccessKeyId, aws.SecretAccessKey, aws.SSL)
    if err != nil {
        return err
    }

    url, err := svc.PresignedPutObject(aws.BucketHost, aws.BucketKey, time.Second * 24 * 60 * 60)
    if err != nil {
        return url, err
    }

    return url
}

Errors:

# command-line-arguments
./main.go:36:9: not enough arguments to return
    have (error)
    want (string, error)
./main.go:41:9: not enough arguments to return
    have (error)
    want (string, error)
./main.go:44:14: no new variables on left side of :=
./main.go:44:14: cannot assign *url.URL to url (type string) in multiple assignment
./main.go:49:5: not enough arguments to return
    have (string)
    want (string, error)
  • 写回答

2条回答 默认 最新

  • doujia6503 2018-02-27 03:25
    关注

    First things first, you should really do the tour of go as JimB said. It takes less than an hour and you would know the answer to this questions.

    Notice that you are declaring url and err on the return declaration of generatePresignedPutUrl() and it doesn't look like that was what you wanted to do. You should change the function declaration from:

    func generatePresignedPutUrl() (url string, err error) {
    

    To:

    func generatePresignedPutUrl() (string, error) { // Without the variable names
    

    Now lets address your errors in order:

    Error 1: not enough arguments to return

    Error is Go are not really cryptic. not enough arguments to return means literally what it says. You are trying to return less arguments than the ones you should.

    In Go you can return more than one value from a function but no matter how much values you return, you always need to return all the values. Your function func generatePresignedPutUrl() (url string, err error) expects a string and an error but in return err you are just returning err which is of type error so you are missing a string, in this case because there was an error in the function return an empty string. To fix it just do: return "", err instead.

    Error 2: no new variables on left side of :=

    Again not really cryptic. It means none of the variables on the left of := are new. The := declares a new variable and then assigns the value to that variable, so if the variable isn't new you get an error when trying to declare it. In this case you declared url on the function declaration and err on the second line of the function. To fix it replace:

    url, err := svc.PresignedPutObject(aws.BucketHost, aws.BucketKey, time.Second * 24 * 60 * 60)
    

    For:

    url, err = svc.PresignedPutObject(aws.BucketHost, aws.BucketKey, time.Second * 24 * 60 * 60)
    

    Error 3: cannot assign *url.URL to url (type string) in multiple assignment

    You are trying to assign a type *url.URL to the variable url which is a string. In Go you can only assign something to a variable if they are from the same type. Here are the docs for the url.URL type. To fix it do:

    rawUrl, err := svc.PresignedPutObject(aws.BucketHost, aws.BucketKey, time.Second * 24 * 60 * 60)
    if err != nil {
        return "", err
    }
    
    return rawUrl.String() // rawUrl is of type *url.URL, so get it's string representation
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!
  • ¥30 vue+element根据数据循环生成多个table,如何实现最后一列 平均分合并
  • ¥20 pcf8563时钟芯片不启振
  • ¥20 pip2.40更新pip2.43时报错