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

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

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看