Common invalid host-port pairs

Cette page n'est pas encore disponible en français, sa traduction est en cours.
Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.

Metadata

ID: go-best-practices/invalid-host-port-pair

Language: Go

Severity: Warning

Category: Best Practices

Description

The host:port pair is invalid. The HTTP server needs to use a valid pair of address and port.

Non-Compliant Code Examples

package main

import ("net")

func main(){
    // Good
    http.ListenAndServe("localhost:8080", nil)
    http.ListenAndServe(":8080", nil)
    http.ListenAndServe(":http", nil)
    http.ListenAndServe("localhost:http", nil)
    http.ListenAndServe("my_server:8080", nil)
    http.ListenAndServe("", nil) // Defaults to ":http"

    // Bad
    http.ListenAndServe("localhost:8080/", nil) 
    http.ListenAndServe("localhost", nil)       

    // Good
    http.ListenAndServeTLS("localhost:8443", "cert.pem", "key.pem", nil)
    http.ListenAndServeTLS(":8443", "cert.pem", "key.pem", nil)
    http.ListenAndServeTLS("localhost:https", "cert.pem", "key.pem", nil)
    http.ListenAndServeTLS("my_server:8443", "cert.pem", "key.pem", nil)
    http.ListenAndServeTLS("", "cert.pem", "key.pem", nil) // Defaults to ":https"

    // Bad
    http.ListenAndServeTLS("localhost:8443/", "cert.pem", "key.pem", nil)
    http.ListenAndServeTLS("localhost", "cert.pem", "key.pem", nil)
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: may/unit-testing