Standard library / net

net.JoinHostPort()

Join host and port, adding brackets for IPv6 literals. Expected stdout: '[2001:db8::1]:443\n'.

Formal Syntax & Structure

go syntaxgo
func JoinHostPort(host, port string) string

Parameters & Attributes

NameTypeDescription
hoststringHost name or IP literal.
portstringPort number or textual port according to the signature.

Return Value

string

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net")
func main() { fmt.Println(net.JoinHostPort("2001:db8::1", "443")) }
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference