Home Backend Development Golang How to concatenate strings in Go language

How to concatenate strings in Go language

Jan 12, 2023 pm 04:25 PM
golang go language Concatenate strings

Methods for splicing strings: 1. Use the " " sign to splice, the syntax "str = str1 str2"; 2. Use the sprintf() function of the fmt package to splice, the syntax "str = fmt.Sprintf("% s%d%s", s1, i, s2)"; 3. Use the join function to splice; 4. Use the WriteString() function of the buffer package to splice; 5. Use the buffer package's Builder() function to splice.

How to concatenate strings in Go language

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Go language has five methods for splicing strings, namely: splicing using number, splicing using sprintf, splicing using join function, splicing using buffer.WriteString function, and splicing using buffer.Builder.

Method 1: Use numbers to splice

str = str1 + str2
Copy after login
  • Use to achieve string splicing, use numbers to splice strings The premise is that everything to be spliced ​​must be of string type. Here, we use the plus sign to concatenate str1 and str2 into the string str.

Example:

package main
import (
	"fmt"
)
func main() {
	//使用+号形式,实现拼接字符串
	str1 := "Hello,"
	str2 := "HaiCoder"
	strHaiCoder := str1 + str2
	fmt.Println("strHaiCoder =", strHaiCoder)
}
Copy after login

How to concatenate strings in Go language

Analysis:

  • First, we define a The string variable is assigned the value "Hello," and then a string variable is defined and the value is "HaiCoder". We use the form of sign to splice the string str1 and the string str2, and assign the splicing result to the variable strHaiCoder.

  • Finally, we use the print() function to print the variable strHaiCoder and find that "Hello, HaiCoder" is output, that is, we realize the splicing of strings through numbers.

Method 2: Use sprintf to splice

str = fmt.Sprintf("%s%d%s", s1, i, s2)
Copy after login
  • Use sprintf to splice strings, you can splice any data type , here, we have spliced ​​the string s1, the integer i and the string s2 together.

Example:

package main
import (
	"fmt"
)
func main() {
	//使用 sprintf,实现拼接字符串和数字
	str1 := "Hello,"
	str2 := "HaiCoder"
	strHaiCoder := fmt.Sprintf("%s %d %s", str1, 1024, str2)
	fmt.Println("strHaiCoder =", strHaiCoder)
}
Copy after login

How to concatenate strings in Go language

Analysis:

  • First, we define a The string variable is assigned the value "Hello," and then a string variable is defined and the value is "HaiCoder".

  • We use fmt.Sprintf to splice the string str1, the number 1024 and the string str2, and assign the splicing result to the variable strHaiCoder.

  • Finally, we use the print() function to print the variable strHaiCoder and find that "Hello, 1024 HaiCoder" is output, that is, we implement string splicing through fmt.Sprintf.

Method 3: Use the join function to splice

var str []string = []string{s1, s2}
s := strings.Join(str, "")
Copy after login
  • Use Join to splice strings, which actually means strings Arrays are connected. Here, we have spliced ​​together all the elements in the string array str.

Example:

package main
import (
	"fmt"
	"strings"
)
func main() {
	//使用 join 函数,实现拼接字符串
	str1 := "Hello,"
	str2 := "HaiCoder"
	var str = []string{str1, str2}
	strHaiCoder := strings.Join(str, "")
	fmt.Println("strHaiCoder =", strHaiCoder)
}
Copy after login

How to concatenate strings in Go language

Analysis:

  • First, we define a The string variable is assigned the value "Hello," and then a string variable is defined and the value is "HaiCoder".

  • Next, we use variables str1 and variable str2 to define a string array. Finally, we use strings.Join to splice string str1 and string str2, and Assign the splicing result to the variable strHaiCoder.

  • Finally, we use the print() function to print the variable strHaiCoder and find that "Hello, HaiCoder" is output, that is, we implement string splicing through strings.Join.

Method 4: Use the buffer.WriteString function to splice

var bt bytes.Buffer
bt.WriteString(s1)
bt.WriteString(s2)
//获得拼接后的字符串
s3 := bt.String()
Copy after login
  • The performance requirements of using buffer.WriteString to splice strings It is much larger than the above methods, so it is not recommended to use it. Here, we have spliced ​​the strings s1 and s2, and assigned the value to the string s3 after splicing.

Example:

package main
import (
	"bytes"
	"fmt"
)
func main() {
	//使用 buffer.WriteString 函数拼接字符串
	str1 := "Hello,"
	str2 := "HaiCoder"
	var bt bytes.Buffer
	bt.WriteString(str1)
	bt.WriteString(str2)
	strHaiCoder := bt.String()
	fmt.Println("strHaiCoder =", strHaiCoder)
}
Copy after login

How to concatenate strings in Go language

Analysis:

  • First, we define a The string variable is assigned the value "Hello," and another string variable is defined, the value is "HaiCoder", and then a variable bt of the bytes.Buffer type is defined.

  • We use the WriteString method of bytes.Buffer to write the variable str1 and variable str2 into the bt variable. Finally, we use the String method of bytes.Buffer to realize the string str1 and The string str2 is spliced, and the splicing result is assigned to the variable strHaiCoder.

Method 5: Use buffer.Builder to splice

var build strings.Builder
build.WriteString(s1)
build.WriteString(s2)
s3 := build.String()
Copy after login
  • 这是官方推荐使用的字符串拼接方法,这里,我们实现了拼接了字符串 s1 和 s2,拼接后赋值给字符串 s3。

示例:

package main
import (
	"fmt"
	"strings"
)
func main() {
	//使用 buffer.Builder 函数拼接字符串
	str1 := "Hello,"
	str2 := "HaiCoder"
	var build strings.Builder
	build.WriteString(str1)
	build.WriteString(str2)
	strHaiCoder := build.String()
	fmt.Println("strHaiCoder =", strHaiCoder)
}
Copy after login

How to concatenate strings in Go language

分析:

  • 首先,我们定义了一个字符串变量,赋值为 “Hello,”,定义了另一个字符串变量,赋值为 “HaiCoder”,接着又定义了一个 strings.Builder 类型的变量 build。

  • 我们使用 strings.Builder 的 WriteString 方法,将变量 str1 和变量 str2 写入 build 变量,最后,我们使用 strings.Builder 的 String 方法,实现了把字符串 str1 和 字符串 str2 进行了拼接,并把拼接结果赋值给变量 strHaiCoder。

【相关推荐:Go视频教程编程教学

The above is the detailed content of How to concatenate strings in Go language. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What is the difference between `var` and `type` keyword definition structure in Go language? What is the difference between `var` and `type` keyword definition structure in Go language? Apr 02, 2025 pm 12:57 PM

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

How to ensure concurrency is safe and efficient when writing multi-process logs? How to ensure concurrency is safe and efficient when writing multi-process logs? Apr 02, 2025 pm 03:51 PM

Efficiently handle concurrency security issues in multi-process log writing. Multiple processes write the same log file at the same time. How to ensure concurrency is safe and efficient? This is a...

See all articles