github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/docs/content/functions/net.md (about)

     1  ---
     2  title: net functions
     3  menu:
     4    main:
     5      parent: functions
     6  ---
     7  
     8  The `net` namespace contains functions that can help deal with network-related
     9  lookups and calculations. Some of these functions return specifically-typed
    10  values that contain additional methods useful for formatting or further
    11  calculations.
    12  
    13  [RFC 4632]: http://tools.ietf.org/html/rfc4632
    14  [RFC 4291]: http://tools.ietf.org/html/rfc4291
    15  [`inet.af/netaddr`]: https://pkg.go.dev/inet.af/netaddr
    16  [`net`]: https://pkg.go.dev/net
    17  
    18  ## `net.LookupIP`
    19  
    20  Resolve an IPv4 address for a given host name. When multiple IP addresses
    21  are resolved, the first one is returned.
    22  
    23  _Added in gomplate [v1.9.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.9.0)_
    24  ### Usage
    25  
    26  ```
    27  net.LookupIP name
    28  ```
    29  ```
    30  name | net.LookupIP
    31  ```
    32  
    33  ### Arguments
    34  
    35  | name | description |
    36  |------|-------------|
    37  | `name` | _(required)_ The hostname to look up. This can be a simple hostname, or a fully-qualified domain name. |
    38  
    39  ### Examples
    40  
    41  ```console
    42  $ gomplate -i '{{ net.LookupIP "example.com" }}'
    43  93.184.216.34
    44  ```
    45  
    46  ## `net.LookupIPs`
    47  
    48  Resolve all IPv4 addresses for a given host name. Returns an array of strings.
    49  
    50  _Added in gomplate [v1.9.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.9.0)_
    51  ### Usage
    52  
    53  ```
    54  net.LookupIPs name
    55  ```
    56  ```
    57  name | net.LookupIPs
    58  ```
    59  
    60  ### Arguments
    61  
    62  | name | description |
    63  |------|-------------|
    64  | `name` | _(required)_ The hostname to look up. This can be a simple hostname, or a fully-qualified domain name. |
    65  
    66  ### Examples
    67  
    68  ```console
    69  $ gomplate -i '{{ join (net.LookupIPs "twitter.com") "," }}'
    70  104.244.42.65,104.244.42.193
    71  ```
    72  
    73  ## `net.LookupCNAME`
    74  
    75  Resolve the canonical name for a given host name. This does a DNS lookup for the
    76  `CNAME` record type. If no `CNAME` is present, a canonical form of the given name
    77  is returned -- e.g. `net.LookupCNAME "localhost"` will return `"localhost."`.
    78  
    79  _Added in gomplate [v1.9.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.9.0)_
    80  ### Usage
    81  
    82  ```
    83  net.LookupCNAME name
    84  ```
    85  ```
    86  name | net.LookupCNAME
    87  ```
    88  
    89  ### Arguments
    90  
    91  | name | description |
    92  |------|-------------|
    93  | `name` | _(required)_ The hostname to look up. This can be a simple hostname, or a fully-qualified domain name. |
    94  
    95  ### Examples
    96  
    97  ```console
    98  $ gomplate -i '{{ net.LookupCNAME "www.amazon.com" }}'
    99  d3ag4hukkh62yn.cloudfront.net.
   100  ```
   101  
   102  ## `net.LookupSRV`
   103  
   104  Resolve a DNS [`SRV` service record](https://en.wikipedia.org/wiki/SRV_record).
   105  This implementation supports the canonical [RFC2782](https://tools.ietf.org/html/rfc2782)
   106  form (i.e. `_Service._Proto.Name`), but other forms are also supported, such as
   107  those served by [Consul's DNS interface](https://www.consul.io/docs/agent/dns.html#standard-lookup).
   108  
   109  When multiple records are returned, this function returns the first.
   110  
   111  A [`net.SRV`](https://golang.org/pkg/net/#SRV) data structure is returned. The
   112  following properties are available:
   113  - `Target` - _(string)_ the hostname where the service can be reached
   114  - `Port` - _(uint16)_ the service's port
   115  - `Priority`, `Weight` - see [RFC2782](https://tools.ietf.org/html/rfc2782) for details
   116  
   117  _Added in gomplate [v1.9.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.9.0)_
   118  ### Usage
   119  
   120  ```
   121  net.LookupSRV name
   122  ```
   123  ```
   124  name | net.LookupSRV
   125  ```
   126  
   127  ### Arguments
   128  
   129  | name | description |
   130  |------|-------------|
   131  | `name` | _(required)_ The service name to look up |
   132  
   133  ### Examples
   134  
   135  ```console
   136  $ gomplate -i '{{ net.LookupSRV "_sip._udp.sip.voice.google.com" | toJSONPretty "  " }}'
   137  {
   138    "Port": 5060,
   139    "Priority": 10,
   140    "Target": "sip-anycast-1.voice.google.com.",
   141    "Weight": 1
   142  }
   143  ```
   144  
   145  ## `net.LookupSRVs`
   146  
   147  Resolve a DNS [`SRV` service record](https://en.wikipedia.org/wiki/SRV_record).
   148  This implementation supports the canonical [RFC2782](https://tools.ietf.org/html/rfc2782)
   149  form (i.e. `_Service._Proto.Name`), but other forms are also supported, such as
   150  those served by [Consul's DNS interface](https://www.consul.io/docs/agent/dns.html#standard-lookup).
   151  
   152  This function returns all available SRV records.
   153  
   154  An array of [`net.SRV`](https://golang.org/pkg/net/#SRV) data structures is
   155  returned. For each element, the following properties are available:
   156  - `Target` - _(string)_ the hostname where the service can be reached
   157  - `Port` - _(uint16)_ the service's port
   158  - `Priority`, `Weight` - see [RFC2782](https://tools.ietf.org/html/rfc2782) for details
   159  
   160  _Added in gomplate [v1.9.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.9.0)_
   161  ### Usage
   162  
   163  ```
   164  net.LookupSRVs name
   165  ```
   166  ```
   167  name | net.LookupSRVs
   168  ```
   169  
   170  ### Arguments
   171  
   172  | name | description |
   173  |------|-------------|
   174  | `name` | _(required)_ The hostname to look up. This can be a simple hostname, or a fully-qualified domain name. |
   175  
   176  ### Examples
   177  
   178  _input.tmpl:_
   179  ```
   180  {{ range (net.LookupSRVs "_sip._udp.sip.voice.google.com") -}}
   181  priority={{.Priority}}/port={{.Port}}
   182  {{- end }}
   183  ```
   184  
   185  ```console
   186  $ gomplate -f input.tmpl
   187  priority=10/port=5060
   188  priority=20/port=5060
   189  ```
   190  
   191  ## `net.LookupTXT`
   192  
   193  Resolve a DNS [`TXT` record](https://en.wikipedia.org/wiki/SRV_record).
   194  
   195  This function returns all available TXT records as an array of strings.
   196  
   197  _Added in gomplate [v1.9.0](https://github.com/hairyhenderson/gomplate/releases/tag/v1.9.0)_
   198  ### Usage
   199  
   200  ```
   201  net.LookupTXT name
   202  ```
   203  ```
   204  name | net.LookupTXT
   205  ```
   206  
   207  ### Arguments
   208  
   209  | name | description |
   210  |------|-------------|
   211  | `name` | _(required)_ The host name to look up |
   212  
   213  ### Examples
   214  
   215  ```console
   216  $ gomplate -i '{{net.LookupTXT "example.com" | data.ToJSONPretty "  " }}'
   217  [
   218    "v=spf1 -all"
   219  ]
   220  ```
   221  
   222  ## `net.ParseAddr`_(unreleased)_
   223  **Unreleased:** _This function is in development, and not yet available in released builds of gomplate._
   224  
   225  Parse the given string as an IP address (a
   226  [`netip.Addr`](https://pkg.go.dev/net/netip#Addr)).
   227  
   228  Any of `netip.Addr`'s methods may be called on the resulting value. See
   229  [the docs](https://pkg.go.dev/net/netip#Addr) for details.
   230  
   231  ### Usage
   232  
   233  ```
   234  net.ParseAddr addr
   235  ```
   236  ```
   237  addr | net.ParseAddr
   238  ```
   239  
   240  ### Arguments
   241  
   242  | name | description |
   243  |------|-------------|
   244  | `addr` | _(required)_ The IP string to parse. It must be either an IPv4 or IPv6 address. |
   245  
   246  ### Examples
   247  
   248  ```console
   249  $ gomplate -i '{{ (net.ParseAddr "192.168.0.1").IsPrivate }}'
   250  true
   251  $ gomplate -i '{{ $ip := net.ParseAddr (net.LookupIP "example.com") -}}
   252    {{ $ip.Prefix 12 }}'
   253  93.176.0.0/12
   254  ```
   255  
   256  ## `net.ParseIP` _(deprecated)_
   257  **Deprecation Notice:** Use [`net.ParseAddr`](#net-parseaddr) instead.
   258  
   259  Parse the given string as an IP address (a `netaddr.IP` from the
   260  [`inet.af/netaddr`](https://pkg.go.dev/inet.af/netaddr) package).
   261  
   262  Any of `netaddr.IP`'s methods may be called on the resulting value. See
   263  [the docs](https://pkg.go.dev/inet.af/netaddr) for details.
   264  
   265  _Added in gomplate [v3.10.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.10.0)_
   266  ### Usage
   267  
   268  ```
   269  net.ParseIP ip
   270  ```
   271  ```
   272  ip | net.ParseIP
   273  ```
   274  
   275  ### Arguments
   276  
   277  | name | description |
   278  |------|-------------|
   279  | `ip` | _(required)_ The IP string to parse. It must be either an IPv4 or IPv6 address. |
   280  
   281  ### Examples
   282  
   283  ```console
   284  $ gomplate -i '{{ (net.ParseIP "192.168.0.1").IsPrivate }}'
   285  true
   286  $ gomplate -i '{{ $ip := net.ParseIP (net.LookupIP "example.com") -}}
   287    {{ $ip.Prefix 12 }}'
   288  93.176.0.0/12
   289  ```
   290  
   291  ## `net.ParsePrefix`_(unreleased)_
   292  **Unreleased:** _This function is in development, and not yet available in released builds of gomplate._
   293  
   294  Parse the given string as an IP address prefix (CIDR) representing an IP
   295  network (a [`netip.Prefix`](https://pkg.go.dev/net/netip#Prefix)).
   296  
   297  The string can be in the form `"192.168.1.0/24"` or `"2001::db8::/32"`,
   298  the CIDR notations defined in [RFC 4632][] and [RFC 4291][].
   299  
   300  Any of `netip.Prefix`'s methods may be called on the resulting value. See
   301  [the docs](https://pkg.go.dev/net/netip#Prefix) for details.
   302  
   303  ### Usage
   304  
   305  ```
   306  net.ParsePrefix prefix
   307  ```
   308  ```
   309  prefix | net.ParsePrefix
   310  ```
   311  
   312  ### Arguments
   313  
   314  | name | description |
   315  |------|-------------|
   316  | `prefix` | _(required)_ The IP address prefix to parse. It must represent either an IPv4 or IPv6 prefix, containing a `/`. |
   317  
   318  ### Examples
   319  
   320  ```console
   321  $ gomplate -i '{{ (net.ParsePrefix "192.168.0.0/24").Range }}'
   322  192.168.0.0-192.168.0.255
   323  $ gomplate -i '{{ $ip := net.ParseAddr (net.LookupIP "example.com") -}}
   324    {{ $net := net.ParsePrefix "93.184.0.0/16" -}}
   325    {{ $net.Contains $ip }}'
   326  true
   327  ```
   328  
   329  ## `net.ParseIPPrefix` _(deprecated)_
   330  **Deprecation Notice:** Use [`net.ParsePrefix`](#net-parseprefix) instead.
   331  
   332  Parse the given string as an IP address prefix (CIDR) representing an IP
   333  network (a `netaddr.IPPrefix` from the
   334  [`inet.af/netaddr`][] package).
   335  
   336  The string can be in the form `"192.168.1.0/24"` or `"2001::db8::/32"`,
   337  the CIDR notations defined in [RFC 4632][] and [RFC 4291][].
   338  
   339  Any of `netaddr.IPPrefix`'s methods may be called on the resulting value.
   340  See [the docs][`inet.af/netaddr`] for details.
   341  
   342  _Added in gomplate [v3.10.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.10.0)_
   343  ### Usage
   344  
   345  ```
   346  net.ParseIPPrefix ipprefix
   347  ```
   348  ```
   349  ipprefix | net.ParseIPPrefix
   350  ```
   351  
   352  ### Arguments
   353  
   354  | name | description |
   355  |------|-------------|
   356  | `ipprefix` | _(required)_ The IP address prefix to parse. It must represent either an IPv4 or IPv6 prefix, containing a `/`. |
   357  
   358  ### Examples
   359  
   360  ```console
   361  $ gomplate -i '{{ (net.ParseIPPrefix "192.168.0.0/24").Range }}'
   362  192.168.0.0-192.168.0.255
   363  $ gomplate -i '{{ $ip := net.ParseIP (net.LookupIP "example.com") -}}
   364    {{ $net := net.ParseIPPrefix "93.184.0.0/16" -}}
   365    {{ $net.Contains $ip }}'
   366  true
   367  $ gomplate -i '{{ $net := net.ParseIPPrefix "93.184.0.0/12" -}}
   368    {{ $net.Range }}'
   369  93.176.0.0-93.191.255.255
   370  ```
   371  
   372  ## `net.ParseRange`_(unreleased)_ _(experimental)_
   373  **Unreleased:** _This function is in development, and not yet available in released builds of gomplate._
   374  **Experimental:** This function is [_experimental_][experimental] and may be enabled with the [`--experimental`][experimental] flag.
   375  
   376  [experimental]: ../config/#experimental
   377  
   378  Parse the given string as an inclusive range of IP addresses from the same
   379  address family (a [`netipx.IPRange`](https://pkg.go.dev/go4.org/netipx#IPRange)
   380  from the [`go4.org/netipx`](https://pkg.go.dev/go4.org/netipx) module).
   381  
   382  The string must contain a hyphen (`-`).
   383  
   384  Any of `netipx.IPRange`'s methods may be called on the resulting value.
   385  See [the docs](https://pkg.go.dev/go4.org/netipx#IPRange) for details.
   386  
   387  Note that this function is experimental for now, because it uses a
   388  [third-party module](https://pkg.go.dev/go4.org/netipx) which may be
   389  brought into the standard library in the future, which may require
   390  breaking changes to this function.
   391  
   392  ### Usage
   393  
   394  ```
   395  net.ParseRange iprange
   396  ```
   397  ```
   398  iprange | net.ParseRange
   399  ```
   400  
   401  ### Arguments
   402  
   403  | name | description |
   404  |------|-------------|
   405  | `iprange` | _(required)_ The IP address range to parse. It must represent either an IPv4 or IPv6 range, containing a `-`. |
   406  
   407  ### Examples
   408  
   409  ```console
   410  $ gomplate -i '{{ (net.ParseRange "192.168.0.0-192.168.0.255").To }}'
   411  192.168.0.255
   412  $ gomplate -i '{{ $range := net.ParseRange "1.2.3.0-1.2.3.233" -}}
   413    {{ $range.Prefixes }}'
   414  [1.2.3.0/25 1.2.3.128/26 1.2.3.192/27 1.2.3.224/29 1.2.3.232/31]
   415  ```
   416  
   417  ## `net.ParseIPRange` _(deprecated)_
   418  **Deprecation Notice:** Use [`net.ParseRange`](#net-parserange) instead.
   419  
   420  Parse the given string as an inclusive range of IP addresses from the same
   421  address family (a `netaddr.IPRange` from the [`inet.af/netaddr`][] package).
   422  
   423  The string must contain a hyphen (`-`).
   424  
   425  Any of `netaddr.IPRange`'s methods may be called on the resulting value.
   426  See [the docs][`inet.af/netaddr`] for details.
   427  
   428  _Added in gomplate [v3.10.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.10.0)_
   429  ### Usage
   430  
   431  ```
   432  net.ParseIPRange iprange
   433  ```
   434  ```
   435  iprange | net.ParseIPRange
   436  ```
   437  
   438  ### Arguments
   439  
   440  | name | description |
   441  |------|-------------|
   442  | `iprange` | _(required)_ The IP address range to parse. It must represent either an IPv4 or IPv6 range, containing a `-`. |
   443  
   444  ### Examples
   445  
   446  ```console
   447  $ gomplate -i '{{ (net.ParseIPRange "192.168.0.0-192.168.0.255").To }}'
   448  192.168.0.255
   449  $ gomplate -i '{{ $range := net.ParseIPRange "1.2.3.0-1.2.3.233" -}}
   450    {{ $range.Prefixes }}'
   451  [1.2.3.0/25 1.2.3.128/26 1.2.3.192/27]
   452  ```
   453  
   454  ## `net.CIDRHost` _(experimental)_
   455  **Experimental:** This function is [_experimental_][experimental] and may be enabled with the [`--experimental`][experimental] flag.
   456  
   457  [experimental]: ../config/#experimental
   458  
   459  Calculates a full host IP address for a given host number within a given IP network address prefix.
   460  
   461  The IP network can be in the form `"192.168.1.0/24"` or `"2001::db8::/32"`,
   462  the CIDR notations defined in [RFC 4632][] and [RFC 4291][].
   463  
   464  Any of `netip.Addr`'s methods may be called on the resulting value. See
   465  [the docs](https://pkg.go.dev/net/netip#Addr) for details.
   466  
   467  _Added in gomplate [v3.11.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.11.0)_
   468  ### Usage
   469  
   470  ```
   471  net.CIDRHost hostnum prefix
   472  ```
   473  ```
   474  prefix | net.CIDRHost hostnum
   475  ```
   476  
   477  ### Arguments
   478  
   479  | name | description |
   480  |------|-------------|
   481  | `hostnum` | _(required)_ Is a whole number that can be represented as a binary integer with no more than the number of digits remaining in the address after the given prefix. |
   482  | `prefix` | _(required)_ Must be given in CIDR notation. It must represent either an IPv4 or IPv6 prefix, containing a `/`. String or [`net.IPNet`](https://pkg.go.dev/net#IPNet) object returned from `net.ParseIPPrefix` can by used. |
   483  
   484  ### Examples
   485  
   486  ```console
   487  $ gomplate -i '{{ "10.12.127.0/20" | net.CIDRHost 268 }}'
   488  10.12.113.12
   489  ```
   490  
   491  ## `net.CIDRNetmask` _(experimental)_
   492  **Experimental:** This function is [_experimental_][experimental] and may be enabled with the [`--experimental`][experimental] flag.
   493  
   494  [experimental]: ../config/#experimental
   495  
   496  The result is a subnet address formatted in the conventional dotted-decimal IPv4 address syntax or hexadecimal IPv6 address syntax, as expected by some software.
   497  
   498  Any of `netip.Addr`'s methods may be called on the resulting value. See
   499  [the docs](https://pkg.go.dev/net/netip#Addr) for details.
   500  
   501  _Added in gomplate [v3.11.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.11.0)_
   502  ### Usage
   503  
   504  ```
   505  net.CIDRNetmask prefix
   506  ```
   507  ```
   508  prefix | net.CIDRNetmask
   509  ```
   510  
   511  ### Arguments
   512  
   513  | name | description |
   514  |------|-------------|
   515  | `prefix` | _(required)_ Must be given in CIDR notation. It must represent either an IPv4 or IPv6 prefix, containing a `/`. String or [`net.IPNet`](https://pkg.go.dev/net#IPNet) object returned from `net.ParseIPPrefix` can by used. |
   516  
   517  ### Examples
   518  
   519  ```console
   520  $ gomplate -i '{{ net.CIDRNetmask "10.12.127.0/20" }}'
   521  255.255.240.0
   522  $ gomplate -i '{{ net.CIDRNetmask "fd00:fd12:3456:7890:00a2::/72" }}'
   523  ffff:ffff:ffff:ffff:ff00::
   524  ```
   525  
   526  ## `net.CIDRSubnets` _(experimental)_
   527  **Experimental:** This function is [_experimental_][experimental] and may be enabled with the [`--experimental`][experimental] flag.
   528  
   529  [experimental]: ../config/#experimental
   530  
   531  Calculates a subnet address within given IP network address prefix.
   532  
   533  Any of `netip.Prefix`'s methods may be called on the resulting values. See
   534  [the docs](https://pkg.go.dev/net/netip#Prefix) for details.
   535  
   536  _Added in gomplate [v3.11.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.11.0)_
   537  ### Usage
   538  
   539  ```
   540  net.CIDRSubnets newbits prefix
   541  ```
   542  ```
   543  prefix | net.CIDRSubnets newbits
   544  ```
   545  
   546  ### Arguments
   547  
   548  | name | description |
   549  |------|-------------|
   550  | `newbits` | _(required)_ Is the number of additional bits with which to extend the prefix. For example, if given a prefix ending in `/16` and a `newbits` value of `4`, the resulting subnet address will have length `/20`. |
   551  | `prefix` | _(required)_ Must be given in CIDR notation. It must represent either an IPv4 or IPv6 prefix, containing a `/`. String or [`net.IPNet`](https://pkg.go.dev/net#IPNet) object returned from `net.ParseIPPrefix` can by used. |
   552  
   553  ### Examples
   554  
   555  ```console
   556  $ gomplate -i '{{ index ("10.0.0.0/16" | net.CIDRSubnets 2) 1 }}'
   557  10.0.64.0/18
   558  $ gomplate -i '{{ net.CIDRSubnets 2 "10.0.0.0/16" -}}'
   559  [10.0.0.0/18 10.0.64.0/18 10.0.128.0/18 10.0.192.0/18]
   560  ```
   561  
   562  ## `net.CIDRSubnetSizes` _(experimental)_
   563  **Experimental:** This function is [_experimental_][experimental] and may be enabled with the [`--experimental`][experimental] flag.
   564  
   565  [experimental]: ../config/#experimental
   566  
   567  Calculates a sequence of consecutive IP address ranges within a particular CIDR prefix.
   568  
   569  Any of `netip.Prefix`'s methods may be called on the resulting values. See
   570  [the docs](https://pkg.go.dev/net/netip#Prefix) for details.
   571  
   572  _Added in gomplate [v3.11.0](https://github.com/hairyhenderson/gomplate/releases/tag/v3.11.0)_
   573  ### Usage
   574  
   575  ```
   576  net.CIDRSubnetSizes newbits... prefix
   577  ```
   578  ```
   579  prefix | net.CIDRSubnetSizes newbits...
   580  ```
   581  
   582  ### Arguments
   583  
   584  | name | description |
   585  |------|-------------|
   586  | `newbits...` | _(required)_ Numbers of additional network prefix bits for returned address range. |
   587  | `prefix` | _(required)_ Must be given in CIDR notation. It must represent either an IPv4 or IPv6 prefix, containing a `/`. String or [`net.IPNet`](https://pkg.go.dev/net#IPNet) object returned from `net.ParseIPPrefix` can by used. |
   588  
   589  ### Examples
   590  
   591  ```console
   592  $ gomplate -i '{{ net.CIDRSubnetSizes 4 4 8 4 "10.1.0.0/16" -}}'
   593  [10.1.0.0/20 10.1.16.0/20 10.1.32.0/24 10.1.48.0/20]
   594  ```