swap two strings (Golang)

huangapple go评论83阅读模式
英文:

swap two strings (Golang)

问题

这段代码没有起作用的原因是,Go语言中的函数参数传递是按值传递的,而不是按引用传递。在函数swap中,str1str2是函数的局部变量,对它们的修改不会影响到main函数中的变量ab。所以,即使在swap函数中交换了str1str2的值,main函数中的ab的值仍然保持不变。

要解决这个问题,你可以将swap函数的参数改为指针类型,这样就可以通过指针修改变量的值。修改后的代码如下:

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

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

这样修改后,swap函数中的参数str1str2是指向main函数中的变量ab的指针,通过指针修改变量的值就可以实现字符串的交换。

英文:

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 ?

答案1

得分: 5

交换str1str2不会改变ab,因为它们是ab的副本。使用指针:

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)
}

点击此处查看示例代码

英文:

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

答案2

得分: 2

这将是惯用的方式。

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)
}
英文:

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)
}

答案3

得分: 1

内置类型作为函数参数时,按值传递,但如果解引用的元素会修改原始值,例如切片(slice)和映射(map)。例如:

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)
}

以上是一个Go语言的示例代码,演示了不同类型作为函数参数时的行为。

英文:

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)
}

答案4

得分: 0

你可以这样写:

a := "你好"
b := "世界"
a, b = b, a
英文:

you can write as that:

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

答案5

得分: 0

package main

import "fmt"

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

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

import "fmt"

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

func main() {
    fmt.Println(swap("Lang", "Go"))
}
英文:
package main
import "fmt"
func swap(a,b string)(string, string){
return b,a
}
func main(){
fmt.Println(swap("Lang","Go"))
}

huangapple
  • 本文由 发表于 2016年2月17日 01:34:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/35439281.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定