[Solved] panic: runtime error: index out of range 1

finally i figure this out XD! type obj struct { Targets []string `json:”targets”` Labels map[string]string `json:”labels”` } func main() { // Creating simulation var myobj = []*obj{} n := new(obj) n.Targets = append(n.Targets, “10.0.0.1”) n.Labels = make(map[string]string) n.Labels[“job”] = “db2” myobj = append(myobj, n) k := new(obj) k.Targets = append(k.Targets, “192.168.1.12”) k.Targets = append(k.Targets, “192.168.1.13”) … Read more

[Solved] How to send JSON body in GET request golang?

Sending a body with a GET request is not supported by HTTP. See this Q&A for full details. But if you really want to do this, even though you know it’s wrong, you can do it this way: iKnowThisBodyShouldBeIgnored := strings.NewReader(“text that won’t mean anything”) req, err := http.NewRequest(http.MethodGet, “http://example.com/foo”, iKnowThisBodyShouldBeIgnored) if err != nil … Read more

[Solved] Continue loop after 404 response

This question leaks details that because it earned so much down-votes. I assumed that we are dealing with a Get method of UserService in go-jira. The Get method returns error: func (s *UserService) Get(username string) (*User, *Response, error) You are omitting error check and pulling it to a blank identifier. Note that if an error … Read more

[Solved] Porting MD5 from node.js to go [closed]

If you only need the standard md5 algorithm, here’s how to use it in go, as noted in the documentation: import ( “fmt” “crypto/md5” “io” ) func main() { h := md5.New() io.WriteString(h, “The fog is getting thicker!”) io.WriteString(h, “And Leon’s getting laaarger!”) fmt.Printf(“%x”, h.Sum(nil)) } If you need an md5 function that returns a … Read more

[Solved] Godep processing custom packages

Thanks to @JimB comments I found out the following: the most obvious and simple solution is just keep your dependencies in GOPATH, with version control, and just let godep handle them all (@JimB) So yes, this means adding package(s) to git and all this stuff. In case if you don’t want/can’t do that, the order … Read more

[Solved] How to extract json api through go to extract stargazers_count of a repo?

Here is an example (completely neglecting error handling): request, _ := http.Get(“https://api.github.com/repos/openebs/openebs”) defer request.Body.Close() bytes, _ := ioutil.ReadAll(request.Body) var apiResponse struct { StargazersCount int `json:”stargazers_count”` } json.Unmarshal(bytes, &apiResponse) fmt.Println(apiResponse.StargazersCount) Playground 3 solved How to extract json api through go to extract stargazers_count of a repo?

[Solved] Sending Requests to Https site

After testing a little bit, it looks like that specific website is using Akamai Ghost and has been configured to block the default go http package user agent. The default user agent appears to be Go-http-client/1.1 If you change your user agent req.Header.Set(“User-Agent”, “my-client-app”) The request will work. However, the website in question appears to … Read more

[Solved] Reading files in bytes [closed]

Thanks to the comments above, the problem is if f, err := os.OpenFile( …) defines a new variable f, this new variable is not used. There are two potential solutions: f, err := os.OpenFile(fn, os.O_RDWR, 0) if err != nil { log.Fatal(“error”, err) } Or var f *os.File var err error if f, err = … Read more

[Solved] ask import undefined Go

function start with lower case is an unexport function. An unexport function can be used only inside the package. You have to export getRoute function by changing the function name to GetRoute. solved ask import undefined Go

[Solved] Filter input amount at specific points in regex? [closed]

[A-Za-z0-9]{1,254} Indicates , any character of: ‘A’ to ‘Z’, ‘a’ to ‘z’, ‘0’ to ‘9’ (between 1 and 254 times (matching the most amount possible)), Edit: If you need @ at the end try with the regex [A-Za-z0-9]{1,254}@ 1 solved Filter input amount at specific points in regex? [closed]