首页 > 其他分享 >【遇到一个神奇的问题】暂未想到原因,http.Post 传入 nil参数正确,但是传输值为 nil 的 *bytes.BytesReader 就 `invalid memory address or

【遇到一个神奇的问题】暂未想到原因,http.Post 传入 nil参数正确,但是传输值为 nil 的 *bytes.BytesReader 就 `invalid memory address or

时间:2023-07-24 11:45:41浏览次数:53  
标签:fuchunzhang github Users nil 值为 暂未 terraform go com

出错的代码如下:

func getEab(ctx context.Context, credentialsJSON string, old *externalAccountKeyResp) (*externalAccountKeyResp, error) {
    // init http client
    //
	var postData *bytes.Reader = nil
	if old != nil {
		buf, _ := json.Marshal(old)
		postData = bytes.NewReader(buf)
	}
	var api = fmt.Sprintf("https://publicca.googleapis.com/v1beta1/projects/%s/locations/global/externalAccountKeys", "xxxx")
	resp, err := conf.Client(context.Background()).Post(api, "application/json", postData)

最后一行代码出现:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x1024b56d0]

goroutine 36 [running]:
bytes.(*Reader).Len(...)
        /opt/homebrew/Cellar/go/1.20.4/libexec/src/bytes/reader.go:27
net/http.NewRequestWithContext({0x102f2d2c8?, 0x140000340b8}, {0x102a565e4?, 0x1035bef20?}, {0x140005b8120?, 0x5e?}, {0x102f24be0, 0x0?})
        /opt/homebrew/Cellar/go/1.20.4/libexec/src/net/http/request.go:909 +0x3e0
net/http.NewRequest(...)
        /opt/homebrew/Cellar/go/1.20.4/libexec/src/net/http/request.go:840
net/http.(*Client).Post(0x102a8fcf2?, {0x140005b8120?, 0x140000340b8?}, {0x102a5fc1e, 0x10}, {0x102f24be0?, 0x0?})
        /opt/homebrew/Cellar/go/1.20.4/libexec/src/net/http/client.go:844 +0x6c
github.com/myklst/terraform-provider-st-gcp/gcp.gcpGetEab({0x102f2d338?, 0x140005a2cf0?}, {0x140005ef500, 0x920}, 0x0)
        /Users/fuchunzhang/code/github.com/ahfuzhang/terraform-provider-st-gcp/gcp/resource_eab.go:223 +0x348
github.com/myklst/terraform-provider-st-gcp/gcp.(*gcpAcmeEabResource).Create(0x0?, {0x102f2d338, 0x140005a2cf0}, {{{{0x102f31510, 0x140005a3c50}, {0x102d37d40, 0x140005a37d0}}, {0x102f32a00, 0x14000622410}}, {{{0x102f31510, ...}, ...}, ...}, ...}, ...)
        /Users/fuchunzhang/code/github.com/ahfuzhang/terraform-provider-st-gcp/gcp/resource_eab.go:83 +0x15c
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).CreateResource(0x14000349b80, {0x102f2d338, 0x140005a2cf0}, 0x14000545380, 0x14000545320)
        /Users/fuchunzhang/go/pkg/mod/github.com/hashicorp/[email protected]/internal/fwserver/server_createresource.go:97 +0x428
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).ApplyResourceChange(0x140005454e0?, {0x102f2d338, 0x140005a2cf0}, 0x140005aa410, 0x140005454e0)
        /Users/fuchunzhang/go/pkg/mod/github.com/hashicorp/[email protected]/internal/fwserver/server_applyresourcechange.go:54 +0x370
github.com/hashicorp/terraform-plugin-framework/internal/proto6server.(*Server).ApplyResourceChange(0x14000349b80, {0x102f2d338?, 0x140005a2ba0?}, 0x140005aa370)
        /Users/fuchunzhang/go/pkg/mod/github.com/hashicorp/[email protected]/internal/proto6server/server_applyresourcechange.go:52 +0x314
github.com/hashicorp/terraform-plugin-go/tfprotov6/tf6server.(*server).ApplyResourceChange(0x140001b4280, {0x102f2d338?, 0x140005a21b0?}, 0x14000264070)
        /Users/fuchunzhang/go/pkg/mod/github.com/hashicorp/[email protected]/tfprotov6/tf6server/server.go:816 +0x3bc
github.com/hashicorp/terraform-plugin-go/tfprotov6/internal/tfplugin6._Provider_ApplyResourceChange_Handler({0x102edc4c0?, 0x140001b4280}, {0x102f2d338, 0x140005a21b0}, 0x14000264000, 0x0)
        /Users/fuchunzhang/go/pkg/mod/github.com/hashicorp/[email protected]/tfprotov6/internal/tfplugin6/tfplugin6_grpc.pb.go:385 +0x170
google.golang.org/grpc.(*Server).processUnaryRPC(0x1400014e000, {0x102f317f8, 0x14000582b60}, 0x140005b4000, 0x1400010f080, 0x1035b16c0, 0x0)
        /Users/fuchunzhang/go/pkg/mod/google.golang.org/[email protected]/server.go:1337 +0xc90
google.golang.org/grpc.(*Server).handleStream(0x1400014e000, {0x102f317f8, 0x14000582b60}, 0x140005b4000, 0x0)
        /Users/fuchunzhang/go/pkg/mod/google.golang.org/[email protected]/server.go:1714 +0x82c
google.golang.org/grpc.(*Server).serveStreams.func1.1()
        /Users/fuchunzhang/go/pkg/mod/google.golang.org/[email protected]/server.go:959 +0x84
created by google.golang.org/grpc.(*Server).serveStreams.func1
        /Users/fuchunzhang/go/pkg/mod/google.golang.org/[email protected]/server.go:957 +0x16c

换成如下的写法后,问题解决:

	if old != nil {
		buf, _ := json.Marshal(old)
		resp, err = conf.Client(context.Background()).Post(api, "application/json", bytes.NewReader(buf))
	} else {
		resp, err = conf.Client(context.Background()).Post(api, "application/json", nil)
	}

暂未想到原因。

标签:fuchunzhang,github,Users,nil,值为,暂未,terraform,go,com
From: https://www.cnblogs.com/ahfuzhang/p/17576812.html

相关文章

  • redis get 返回nil
    RedisGet返回Nil在使用Redis进行开发时,经常会遇到GET命令返回nil的情况。这种情况可能让开发者感到困惑,不知道为什么无法获取到想要的值。本文将介绍Redis的GET命令,解释为什么它会返回nil,以及如何处理这种情况。Redis简介Redis是一个内存数据存储系统,它可以用作......
  • 如何设置 antd protable中的搜索下拉框的宽度值为130
    在AntDesignProTable中,设置搜索栏下拉框的宽度稍有不同。受控于valueType属性中的select或其它类型,你可以在列定义中提供一个request函数来定义下拉选项,并且使用fieldProps来修改下拉选择的一些属性。这就是如何在ProTable列定义中设置一个下拉选择的宽度:importP......
  • golang中的空切片和nil切片
    Golang中的切片是很基本的数据结构,它的底层是一个数组,表层是一个包含三个变量的结构体:typeslicestruct{arrayunsafe.Pointer //指针,指向底层的数组lengthint //切片的长度capcityint //切片的容量}当把一个切片赋值给另一个切片时,实际上是对切片表层结构体的浅......
  • 前端封装方法 去掉值为空i字符串的字段
    1.import_from'lodash'2.functionclearEmptyParam(config){ ['data','params'].forEach(item=>{  if(config[item]){   constkeys=Object.keys(config[item])   if(keys.length){    keys.forEach(key=......
  • 快消EDI:联合利华Unilever EDI需求分析
    联合利华(Unilever)是一家跨国消费品公司,总部位于英国和荷兰,在全球范围内经营着众多知名品牌,涵盖了食品、饮料、清洁剂、个人护理产品等多个领域。作为一家跨国公司,联合利华在全球各地都有业务和生产基地,公司的产品畅销于全球各个市场,并持续创新以适应不同地区的消费者需求和文化特......
  • 【并发编程】为什么Hashtable和ConcurrentHashMap 是不允许键或值为 null 的,HashMap
    原文链接:https://blog.csdn.net/cy973071263/article/details/126354336目录一、从源码的角度分析原因1.1Hashtable1.2ConcurrentHashMap1.3HashMap二、从架构设计的角度分析原因2.1为何不支持null值?2.1.1ConcurrentHashMap2.1.2Hashtable2.1.3HashMap2.2......
  • 定义一个长度为10 的数组并赋值为0-9
    一、使用Array.applyletarr=Array.apply(null,{length:10}).map((item,index)=>{   returnindex;   });console.log(arr);//(10)[0,1,2,3,4,5,6,7,8,9]//原理:Array.apply的第二个参数是类数组调用Array.apply(null,{length:10})等于生成了长......
  • Objective C中NULL、Nil、nil、NSNull 的区别
    NULLValuesThesemacrosdefinenullvaluesforclassesandinstances.NULL可以用在C语言的各种指针上#define__DARWIN_NULL#define__DARWIN_NULLConstantsc特有的,例子:int*pointerToInt=NULL;char*pointerToChar=NULL;structTreeNode*roo......
  • postgresql json取值为何这么慢?
    一、缘起慢sql分析,总行数80w+,通过监控分析慢SQL,某个查询耗时超1s。比较特殊的是:其中有个字段info是jsonb类型,写法:info::json->'length'aslength同样的查询条件查这个字段和不查这个字段相差3.3倍那看来就是json取值拖垮了查询的性能。取jsonb中的字段有多种取法(如下),那......
  • printf的返回值为int类型,即为当前打印字符的个数
    //printfRet.cpp:Definestheentrypointfortheconsoleapplication.//#include"stdafx.h"intmain(intargc,char*argv[]){ intret=0; inta=104;ret=printf("%d",a);//printf将返回3, printf("\n%d\n",ret);......