Skip to content

Commit

Permalink
docs/howto: use strconv.Atoi to convert string to int
Browse files Browse the repository at this point in the history
This adds a Commented CUE guide demonstrating how to use the built-in
function strconv.Atoi to convert a string representation of an int to
the underlying int.

Preview-Path: /docs/howto/use-the-built-in-function-strconv-atoi-to-convert-strings-to-ints/
Signed-off-by: Jonathan Matthews <[email protected]>
Change-Id: I72a43c0d6b122688fcb287ea5e721d82f8ea44d9
Dispatch-Trailer: {"type":"trybot","CL":1174211,"patchset":7,"ref":"refs/changes/11/1174211/7","targetBranch":"alpha"}
  • Loading branch information
jpluscplusm authored and cueckoo committed Jan 23, 2024
1 parent d559efe commit 471d6b9
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: Using the built-in function "strconv.Atoi" to convert strings to ints
tags:
- commented cue
- encodings
authors:
- jpluscplusm
toc_hide: true
---

This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}})
demonstrates how to use the built-in function
[`strconv.Atoi`](https://pkg.go.dev/cuelang.org/go/pkg/strconv#Atoi)
to convert a string representation of an int to the number itself, using base
10.

{{{with code "en" "cc"}}}
exec cue export
cmp stdout out
-- file.cue --
package example

import "strconv"

"0": strconv.Atoi("0")
"1": strconv.Atoi("1")
"10": strconv.Atoi("10")
"42": strconv.Atoi("42")
"-42": strconv.Atoi("-42")
"050": strconv.Atoi("050")
"-050": strconv.Atoi("-050")
"00012345": strconv.Atoi("00012345")
-- out --
{
"0": 0,
"1": 1,
"10": 10,
"42": 42,
"-42": -42,
"050": 50,
"-050": -50,
"00012345": 12345
}
{{{end}}}

## Related content

- [`strconv.Atoi`](https://pkg.go.dev/cuelang.org/[email protected]/pkg/strconv#Atoi)
is a base-10 convenience wrapper around the built-in function
[`strconv.ParseInt`](https://pkg.go.dev/cuelang.org/[email protected]/pkg/strconv#ParseInt),
which can perform conversions with custom bases and bit widths
- The [`strconv`](https://pkg.go.dev/cuelang.org/go/pkg/strconv) built-in package
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package site
{
content: {
docs: {
howto: {
"use-the-built-in-function-strconv-atoi-to-convert-strings-to-ints": {
page: {
cache: {
code: {
cc: "kpzS0EdTgXLpgY70txNtMTwIjplDxaGKmtIaFTePj+M="
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package site

content: docs: howto: "use-the-built-in-function-strconv-atoi-to-convert-strings-to-ints": {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
title: Using the built-in function "strconv.Atoi" to convert strings to ints
tags:
- commented cue
- encodings
authors:
- jpluscplusm
toc_hide: true
---

This [Commented CUE]({{< relref "docs/howto#commented-cue-guides" >}})
demonstrates how to use the built-in function
[`strconv.Atoi`](https://pkg.go.dev/cuelang.org/go/pkg/strconv#Atoi)
to convert a string representation of an int to the number itself, using base
10.

{{< code-tabs >}}
{{< code-tab name="file.cue" language="cue" area="top-left" >}}
package example

import "strconv"

"0": strconv.Atoi("0")
"1": strconv.Atoi("1")
"10": strconv.Atoi("10")
"42": strconv.Atoi("42")
"-42": strconv.Atoi("-42")
"050": strconv.Atoi("050")
"-050": strconv.Atoi("-050")
"00012345": strconv.Atoi("00012345")
{{< /code-tab >}}
{{< code-tab name="TERMINAL" language="" type="terminal" area="top-right" >}}
$ cue export
{
"0": 0,
"1": 1,
"10": 10,
"42": 42,
"-42": -42,
"050": 50,
"-050": -50,
"00012345": 12345
}
{{< /code-tab >}}
{{< /code-tabs >}}

## Related content

- [`strconv.Atoi`](https://pkg.go.dev/cuelang.org/[email protected]/pkg/strconv#Atoi)
is a base-10 convenience wrapper around the built-in function
[`strconv.ParseInt`](https://pkg.go.dev/cuelang.org/[email protected]/pkg/strconv#ParseInt),
which can perform conversions with custom bases and bit widths
- The [`strconv`](https://pkg.go.dev/cuelang.org/go/pkg/strconv) built-in package

0 comments on commit 471d6b9

Please sign in to comment.