I am getting the below runtime error in Go:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x10 pc=0x63352d]
goroutine 6 [running]:
net/http.redirectBehavior(0x89a7c5, 0x5, 0x0, 0xc0001d8000, 0x0, 0x0, 0x0)
c:/Go/src/net/http/client.go:422 +0x2d
net/http.(*Client).do(0xc000180210, 0xc0001d8000, 0x0, 0x0, 0x0)
c:/Go/src/net/http/client.go:655 +0x327
net/http.(*Client).Do(0xc000180210, 0xc0001d8000, 0xc, 0x8a4095, 0x1b)
c:/Go/src/net/http/client.go:509 +0x3c
my golang source code which creates TFS bug using Rest API as follows:
package main
import (
"encoding/json"
"log"
"net/http"
"io/ioutil"
"crypto/tls"
"bytes"
"fmt"
httpntlm "github.com/vadimi/go-http-ntlm"
)
type DData struct {
Title string `json:"title"`
}
type CBug struct {
Op string `json:"op"`
Path string `json:"path"`
Value interface{} `json:"value"`
}
type CArray []CBug
func createDefectJson(title string, id int, worktype string) CArray {
var bug = CArray{
CBug{
"add",
"/fields/System.Title",
title,
},
CBug{
"add",
"/fields/System.Id",
id,
},
CBug{
"add",
"/fields/xxx.WorkType",
worktype,
},
}
return bug
}
func main() {
http.HandleFunc("/",httpHandler)
http.ListenAndServe(":80",nil)
}
func httpHandler (w http.ResponseWriter, r *http.Request) {
Defect := DData{}
jsn, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal("Error While Reading r.Body: ",err)
}
err = json.Unmarshal(jsn,&Defect)
if err != nil {
log.Fatal("Error While Reading r.Body: ",err)
}
createbug()
}
func createbug() {
client := http.Client{
Transport: &httpntlm.NtlmTransport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Domain: "x",
User: "xxxx",
Password: "xxx",
},
}
bug := createDefectJson("Title",1,"WorkType")
defectJson, err := json.Marshal(bug)
if err != nil {
log.Printf("Cannot encode to JSON ", err)
}
log.Println("Json to createBug",defectJson)
req, err := http.NewRequest("PATCH", "http://tfs.com:8080/tfs/xxx/ProjectName/_apis/wit/workitems/$Bug?api-version=1.0", bytes.NewBuffer(defectJson))
req.Header.Set("Content-Type","application/json-patch+json")
resp, err := client.Do(req)
if err != nil {
log.Println(err)
}
respBody,_ := ioutil.ReadAll(resp.Body)
fmt.Println("BugResponse",string(respBody))
defer resp.Body.Close()
}
The above worked fine for the few times and then started giving runtime error in golang and then never succeeded.
my go env:
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\iqadm\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\iqadm\go
set GOPROXY=
set GORACE=
set GOROOT=c:\Go
set GOTMPDIR=
set GOTOOLDIR=c:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\IVC170~1\AppData\Local\Temp\go-build771991354=/tmp/go-build -gno-record-gcc-switches
I am not sure how can we resolve.
User contributions licensed under CC BY-SA 3.0