在Go包中,将变量替换为空白标识符。

huangapple go评论79阅读模式
英文:

Replace variable with a blank identifier in Go package

问题

当我在第25行替换变量resp时,我得到了错误:=左侧没有新变量,代码无法编译。我该如何用空标识符替换变量以丢弃响应?

谢谢您的提前帮助!

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)

func main() {

	cred, err := azidentity.NewDefaultAzureCredential(nil)
	if err != nil {
		log.Fatalf("Authentication failure: %+v", err)
	}

	ctx := context.Background()
	//data := "Hello world!"
	blobURL := "https://STORGEACCOUNT.blob.core.windows.net/CONTAINER/BLOB.txt"

	blockBlob, err := azblob.NewBlockBlobClient(blobURL, cred, nil)

	_, err = blockBlob.Delete(ctx, nil)

	if err != nil {
		log.Fatalf("Failure: %+v", err)
	}

	fmt.Println("删除成功")
}
英文:

When I replace the variable resp on line 25 I get the error no new variables on left side of := and the code won't compile. How can I replace the variable with a blank identifer to throw away the response?

Thank you in advance!

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
	"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
)

func main() {

	cred, err := azidentity.NewDefaultAzureCredential(nil)
	if err != nil {
		log.Fatalf("Authentication failure: %+v", err)
	}

	ctx := context.Background()
	//data := "Hello world!"
	blobURL := "https://STORGEACCOUNT.blob.core.windows.net/CONTAINER/BLOB.txt"

	blockBlob, err := azblob.NewBlockBlobClient(blobURL, cred, nil)

	resp, err := blockBlob.Delete(ctx, nil)

	if err != nil {
		log.Fatalf("Failure: %+v", err)
	}

	fmt.Println(resp)
}

答案1

得分: 1

短形式声明要求你至少声明一个新变量。当你用_替换resp时,这个要求就不再满足,因为err已经被定义了。所以:

    _, err = blockBlob.Delete(ctx, nil)
英文:

Short-form declarations require you to declare at least one new variable. When you replace resp with _, this is no longer satisfied because err is already defined. So:

    _, err = blockBlob.Delete(ctx, nil)

huangapple
  • 本文由 发表于 2021年11月30日 00:52:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/70158349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定