String Byte Slice Conversion Copies
Converting between string and []byte copies the data each way, so byte-slice edits never touch the original string, and len counts UTF-8 bytes not runes.
Code
package main
import "fmt"
func main() {
s := "café"
// string → []byte 会发生一次完整拷贝:改字节不影响原字符串
b := []byte(s)
b[1] = 'o'
fmt.Println("original:", s, "modified:", string(b))
// []byte → string 同样是一次拷贝,得到独立的新值
back := string(b)
fmt.Println("converted:", back)
// 字符串不可变:没有合法途径原地改写字节;len 按 UTF-8 字节计
fmt.Println("utf8 bytes:", len(s), "runes:", len([]rune(s)))
}What the code does
Count the function main as the entire surface area of String Byte Slice Conversion Copies.
Applying it
Running the file prints labeled results such as original:, converted:, utf8 bytes: — each operation’s effect is visible in the output.
Statements that matter
back := string(b)
Interactive Tools for This Topic
A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.Byte Size ConverterShow one byte count in every unit side by side, using decimal (kB/MB/GB) or binary (KiB/MiB/GiB) standards.CSS Unit ConverterConvert a length between px, rem, em, pt, pc, in, cm, mm, and percent using a configurable root font size, following the 96dpi CSS definition.cURL to Python & Go ConverterTranslate curl CLI commands with headers, auth, JSON bodies, and timeouts into ready-to-run Python requests and Go net/http client code snippets.
FAQ
- What does String Byte Slice Conversion Copies print when run?
- Running the file directly is enough — expect labeled output lines such as `original:`, `converted:`, `utf8 bytes:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.