C
C
coolstudent2018-09-12 11:33:48
go
coolstudent, 2018-09-12 11:33:48

Golang how to get http server start error for example if port is busy?

subject the docs say that ListenAndServe always returns a non-nil error. but I did not find the values ​​of the errors of the operation of the server itself there, I found only errors in servicing requests.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Pavlyuk, 2018-09-12
@coolstudent

Everything returns successfully from ListenAndServe

package main

import (
  "fmt"
  "net/http"
)

func main() {
  err := http.ListenAndServe(":8090", nil)
  fmt.Println("Error creating http server: ", err)
}

$ go run main.go 
Error creating http server:  listen tcp :8090: bind: address already in use

E
evgensoft, 2018-09-12
@evgensoft

log.Fatal(http.ListenAndServe(":8080", nil)) // throws an error and exits if it can't bind the port

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question