W języku Go ciąg znaków jest niezmiennym łańcuchem dowolnych bajtów zakodowanych przy użyciu kodowania UTF-8. W ciągach Go proces dodawania dwóch lub więcej ciągów do nowego pojedynczego ciągu nazywany jest konkatenacją. Najprostszym sposobem łączenia dwóch lub więcej ciągów znaków w języku Go jest użycie operatora +. Nazywa się go także operatorem konkatenacji.
Przykład:
C
null sprawdzanie w Javie
// Go program to illustrate> // how to concatenate strings> package main> import 'fmt'> func main() {> >// Creating and initializing strings> >// using var keyword> >var str1 string> >str1 = 'Welcome!'> >var str2 string> >str2 = 'techcodeview.com'> >// Concatenating strings> >// Using + operator> >fmt.Println('New string 1: ', str1+str2)> >// Creating and initializing strings> >// Using shorthand declaration> >str3 := 'Geeks'> >str4 := 'Geeks'> >// Concatenating strings> >// Using + operator> >result := str3 + '>for>' + str4> >fmt.Println('New string 2: ', result)> }> |
>
>
Wyjście:
New string 1: Welcome!techcodeview.com New string 2: techcodeview.com>
Inne metody łączenia ciągów
- Korzystanie z bytes.Buffer: Możesz także utworzyć ciąg znaków, łącząc bajty ciągów za pomocą bajtów.Buffer z Metoda WriteString(). . Jest zdefiniowany w pakiecie bytes. Zapobiega generowaniu niepotrzebnego obiektu string, czyli nie generuje nowego stringa jak operator in + z dwóch lub więcej stringów. Przykład:
C
// Go program to illustrate how to concatenate strings> // Using bytes.Buffer with WriteString() function> package main> import (> >'bytes'> >'fmt'> )> func main() {> >// Creating and initializing strings> >// Using bytes.Buffer with> >// WriteString() function> >var b bytes.Buffer> > >b.WriteString('G')> >b.WriteString('e')> >b.WriteString('e')> >b.WriteString('k')> >b.WriteString('s')> > >fmt.Println('String: ', b.String())> >b.WriteString('f')> >b.WriteString('o')> >b.WriteString('r')> >b.WriteString('G')> >b.WriteString('e')> >b.WriteString('e')> >b.WriteString('k')> >b.WriteString('s')> > >fmt.Println('String: ', b.String())> }> |
>
och, przesunięcie ku czerwieni
>
- Wyjście:
String: Geeks String: techcodeview.com>
- Używanie Sprintf: W języku Go możesz także łączyć ciągi znaków za pomocą Sprintf() metoda. Przykład:
C
// Go program to illustrate how to concatenate strings> // Using Sprintf function> package main> import 'fmt'> func main() {> >// Creating and initializing strings> >str1 := 'Tutorial'> >str2 := 'of'> >str3 := 'Go'> >str4 := 'Language'> >// Concatenating strings using> >// Sprintf() function> >result := fmt.Sprintf('%s%s%s%s', str1,> >str2, str3, str4)> > >fmt.Println(result)> }> |
>
tabela ascii w c
>
- Wyjście:
TutorialofGoLanguage>
- Używanie operatora += lub dołączania ciągu: W Go strings możesz dołączać ciąg znaków za pomocą += operator . Operator ten dodaje nowy lub podany ciąg na końcu określonego ciągu. Przykład:
C
// Go program to illustrate how> // to concatenate strings> // Using += operator> package main> import 'fmt'> func main() {> >// Creating and initializing strings> >str1 := 'Welcome'> >str2 := 'techcodeview.com'> >// Using += operator> >str1 += str2> >fmt.Println('String: ', str1)> >str1 += 'This is the tutorial of Go language'> >fmt.Println('String: ', str1)> >str2 += 'Portal'> >fmt.Println('String: ', str2)> }> |
>
>
- Wyjście:
String: Welcometechcodeview.com String: Welcometechcodeview.comThis is the tutorial of Go language String: techcodeview.comPortal>
- Korzystanie z funkcji Join(): Ta funkcja łączy wszystkie elementy znajdujące się w fragmencie ciągu w jeden ciąg. Ta funkcja jest dostępna w pakiecie string. Składnia:
func Join(str []string, sep string) string>
- Tutaj, ul to ciąg, z którego możemy łączyć elementy, a sep to separator umieszczany pomiędzy elementami końcowego ciągu. Przykład:
C
// Go program to illustrate how to> // concatenate all the elements> // present in the slice of the string> package main> import (> >'fmt'> >'strings'> )> func main() {> >// Creating and initializing slice of string> >myslice := []string{'Welcome', 'To',> >'techcodeview.com', 'Portal'}> >// Concatenating the elements> >// present in the slice> >// Using join() function> >result := strings.Join(myslice, '-')> >fmt.Println(result)> }> |
>
>
- Wyjście:
Welcome-To-techcodeview.com-Portal>
- Używanie strings.Builder: Możesz także utworzyć ciąg poprzez połączenie ciągów za pomocą strings.Builder za pomocą Metoda WriteString(). Jest zdefiniowany w pakiecie strings. Zużywa mniej pamięci podczas łączenia ciągów i jest lepszym sposobem łączenia. Funkcja Zapisz ciąg dołącza zawartość ciągu do bufora i umożliwia szybsze łączenie ciągów. Zapobiega generowaniu niepotrzebnego obiektu string, czyli nie generuje nowego stringa jak operator in + z dwóch lub więcej stringów.
Przykład:
Iść
gzip dla Linuksa
// Go program to illustrate how to concatenate strings> // Using strings.Builder with WriteString() function> package> main> import> (> >'fmt'> >'strings'> )> func> main() {> >// Creating and initializing strings> >// Using strings.Builder with> >// WriteString() function> >var> str strings.Builder> >str.WriteString(>'Welcome'>)> >fmt.Println(>'String: '>, str.String())> >str.WriteString(>' to'>)> >str.WriteString(>' techcodeview.com!'>)> >fmt.Println(>'String: '>, str.String())> }> |
>
>
Wyjście:
String: Welcome String: Welcome to techcodeview.com!>