albttx
albttx

Reputation: 3794

swap two strings (Golang)

I am currently learning Golang, and i decided to write few simple algorithm for learning the syntax. i hope it's not already answers but i didn't found it ..

I have a problem for swapping string

func swap(str1, str2 string) {
    /*
     * Also possible :
     * str1, str2 = str2, str1
     */
    // str1, str2 = str2, str1
    tmp := str1
    str1 = str2
    str2 = tmp
}

func main() {
    a := "World !"
    b := "Hello"
    swap(a, b)
    fmt.Printf("a=%s\nb=%s\n", a, b)
}

Why this code didn't work ?

Upvotes: 1

Views: 2792

Answers (5)

borey
borey

Reputation: 5

Built-in types as function arguments are passed by value, but if the elements for dereference will modify the original value, such as slice, map. e.g.

package main

import (
    "bytes"
    "fmt"
    "strings"
)

func f_1(a int) {
    a = 2
}

func f_1_1(a *int) {
    *a = 2
}

func f_2(s string) {
    s = "cba"
}

func f_2_1(s *string) {
    *s = "cba"
}

func f_3(v []string) {
    v[0] = "haha"
}

func f_3_1(v []string) {
    v = nil
}

func f_3_2(v *[]string) {
    *v = nil
}

func f_4(m map[int]int) {
    m[1] = 3
    m[3] = 1
}

func f_4_1(m map[int]int) {
    m = nil
}

func f_4_2(m *map[int]int) {
    *m = nil
}

func f_5(b []byte) {
    b[0] = 0x40
}

func f_5_1(b []byte) {
    b = bytes.Replace(b, []byte("1"), []byte("a"), -1)
}

func f_5_2(b *[]byte) {
    *b = bytes.Replace(*b, []byte("1"), []byte("a"), -1)
}

type why struct {
    s []string
}

func (ss why) SetV(s []string) {
    ss.s = s
}

func (ss *why) SetP(s []string) {
    ss.s = s
}

func (ss why) String() string {
    return strings.Join(ss.s, ",")
}

func main() {
    a := 1
    s := "abc"
    v := []string{"sd", "aa"}
    m := map[int]int{1: 1, 2: 2, 3: 3}
    f_1(a)
    f_2(s)
    f_3(v)
    f_4(m)
    fmt.Printf("%d,%s,%v,%v\n", a, s, v, m)
    f_3_1(v)
    f_4_1(m)
    fmt.Printf("%d,%s,%v,%v\n", a, s, v, m)
    f_1_1(&a)
    f_2_1(&s)
    f_3_2(&v)
    f_4_2(&m)
    fmt.Printf("%d,%s,%v,%v\n", a, s, v, m)
    b := []byte("12145178")
    f_5(b)
    fmt.Printf("%s\n", b)
    f_5_1(b)
    fmt.Printf("%s\n", b)
    f_5_2(&b)
    fmt.Printf("%s\n", b)
    ss := &why{}
    ss.SetV([]string{"abc", "efg"})
    fmt.Println(ss)
    ss.SetP([]string{"abc", "efg"})
    fmt.Println(ss)
}

Upvotes: 1

Anchal Sarraf
Anchal Sarraf

Reputation: 3909

package main

import "fmt"

func swap(a,b string)(string, string){
return b,a
}

func main(){
fmt.Println(swap("Lang","Go"))
}

Upvotes: 0

acf
acf

Reputation: 1

you can write as that:

a := "hello"
b := "world"
a, b = b, a

Upvotes: 0

harold ramos
harold ramos

Reputation: 651

This will be the idiomatic way.

package main

import "fmt"

func swap(a, b string)(string, string) {
    return b, a
}

func main() {
    f, s := swap("world" , "hello")
    fmt.Println(f, s)
}

Upvotes: 1

Mr_Pink
Mr_Pink

Reputation: 109399

Swapping str1 and str2 doesn't change a and b, because they are copies of a and b. Use pointers:

func swap(str1, str2 *string) {
    *str1, *str2 = *str2, *str1
}

func main() {
    a := "salut"
    b := "les gens"
    swap(&a, &b)
    fmt.Printf("a=%s\nb=%s\n", a, b)
}

http://play.golang.org/p/Qw0t5I-XGT

Upvotes: 6

Related Questions