swap two strings (Golang)

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

swap two strings (Golang)

问题

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

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

  1. func swap(str1, str2 *string) {
  2. tmp := *str1
  3. *str1 = *str2
  4. *str2 = tmp
  5. }
  6. func main() {
  7. a := "World !"
  8. b := "Hello"
  9. swap(&a, &b)
  10. fmt.Printf("a=%s\nb=%s\n", a, b)
  11. }

这样修改后,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

  1. func swap(str1, str2 string) {
  2. /*
  3. * Also possible :
  4. * str1, str2 = str2, str1
  5. */
  6. // str1, str2 = str2, str1
  7. tmp := str1
  8. str1 = str2
  9. str2 = tmp
  10. }
  11. func main() {
  12. a := "World !"
  13. b := "Hello"
  14. swap(a, b)
  15. fmt.Printf("a=%s\nb=%s\n", a, b)
  16. }

Why this code didn't work ?

答案1

得分: 5

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

  1. func swap(str1, str2 *string) {
  2. *str1, *str2 = *str2, *str1
  3. }
  4. func main() {
  5. a := "salut"
  6. b := "les gens"
  7. swap(&a, &b)
  8. fmt.Printf("a=%s\nb=%s\n", a, b)
  9. }

点击此处查看示例代码

英文:

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

  1. func swap(str1, str2 *string) {
  2. *str1, *str2 = *str2, *str1
  3. }
  4. func main() {
  5. a := "salut"
  6. b := "les gens"
  7. swap(&a, &b)
  8. fmt.Printf("a=%s\nb=%s\n", a, b)
  9. }

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

答案2

得分: 2

这将是惯用的方式。

  1. package main
  2. import "fmt"
  3. func swap(a, b string)(string, string) {
  4. return b, a
  5. }
  6. func main() {
  7. f, s := swap("world" , "hello")
  8. fmt.Println(f, s)
  9. }
英文:

This will be the idiomatic way.

  1. package main
  2. import "fmt"
  3. func swap(a, b string)(string, string) {
  4. return b, a
  5. }
  6. func main() {
  7. f, s := swap("world" , "hello")
  8. fmt.Println(f, s)
  9. }

答案3

得分: 1

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

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strings"
  6. )
  7. func f_1(a int) {
  8. a = 2
  9. }
  10. func f_1_1(a *int) {
  11. *a = 2
  12. }
  13. func f_2(s string) {
  14. s = "cba"
  15. }
  16. func f_2_1(s *string) {
  17. *s = "cba"
  18. }
  19. func f_3(v []string) {
  20. v[0] = "haha"
  21. }
  22. func f_3_1(v []string) {
  23. v = nil
  24. }
  25. func f_3_2(v *[]string) {
  26. *v = nil
  27. }
  28. func f_4(m map[int]int) {
  29. m[1] = 3
  30. m[3] = 1
  31. }
  32. func f_4_1(m map[int]int) {
  33. m = nil
  34. }
  35. func f_4_2(m *map[int]int) {
  36. *m = nil
  37. }
  38. func f_5(b []byte) {
  39. b[0] = 0x40
  40. }
  41. func f_5_1(b []byte) {
  42. b = bytes.Replace(b, []byte("1"), []byte("a"), -1)
  43. }
  44. func f_5_2(b *[]byte) {
  45. *b = bytes.Replace(*b, []byte("1"), []byte("a"), -1)
  46. }
  47. type why struct {
  48. s []string
  49. }
  50. func (ss why) SetV(s []string) {
  51. ss.s = s
  52. }
  53. func (ss *why) SetP(s []string) {
  54. ss.s = s
  55. }
  56. func (ss why) String() string {
  57. return strings.Join(ss.s, ",")
  58. }
  59. func main() {
  60. a := 1
  61. s := "abc"
  62. v := []string{"sd", "aa"}
  63. m := map[int]int{1: 1, 2: 2, 3: 3}
  64. f_1(a)
  65. f_2(s)
  66. f_3(v)
  67. f_4(m)
  68. fmt.Printf("%d,%s,%v,%v\n", a, s, v, m)
  69. f_3_1(v)
  70. f_4_1(m)
  71. fmt.Printf("%d,%s,%v,%v\n", a, s, v, m)
  72. f_1_1(&a)
  73. f_2_1(&s)
  74. f_3_2(&v)
  75. f_4_2(&m)
  76. fmt.Printf("%d,%s,%v,%v\n", a, s, v, m)
  77. b := []byte("12145178")
  78. f_5(b)
  79. fmt.Printf("%s\n", b)
  80. f_5_1(b)
  81. fmt.Printf("%s\n", b)
  82. f_5_2(&b)
  83. fmt.Printf("%s\n", b)
  84. ss := &why{}
  85. ss.SetV([]string{"abc", "efg"})
  86. fmt.Println(ss)
  87. ss.SetP([]string{"abc", "efg"})
  88. fmt.Println(ss)
  89. }

以上是一个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.

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "strings"
  6. )
  7. func f_1(a int) {
  8. a = 2
  9. }
  10. func f_1_1(a *int) {
  11. *a = 2
  12. }
  13. func f_2(s string) {
  14. s = "cba"
  15. }
  16. func f_2_1(s *string) {
  17. *s = "cba"
  18. }
  19. func f_3(v []string) {
  20. v[0] = "haha"
  21. }
  22. func f_3_1(v []string) {
  23. v = nil
  24. }
  25. func f_3_2(v *[]string) {
  26. *v = nil
  27. }
  28. func f_4(m map[int]int) {
  29. m[1] = 3
  30. m[3] = 1
  31. }
  32. func f_4_1(m map[int]int) {
  33. m = nil
  34. }
  35. func f_4_2(m *map[int]int) {
  36. *m = nil
  37. }
  38. func f_5(b []byte) {
  39. b[0] = 0x40
  40. }
  41. func f_5_1(b []byte) {
  42. b = bytes.Replace(b, []byte("1"), []byte("a"), -1)
  43. }
  44. func f_5_2(b *[]byte) {
  45. *b = bytes.Replace(*b, []byte("1"), []byte("a"), -1)
  46. }
  47. type why struct {
  48. s []string
  49. }
  50. func (ss why) SetV(s []string) {
  51. ss.s = s
  52. }
  53. func (ss *why) SetP(s []string) {
  54. ss.s = s
  55. }
  56. func (ss why) String() string {
  57. return strings.Join(ss.s, ",")
  58. }
  59. func main() {
  60. a := 1
  61. s := "abc"
  62. v := []string{"sd", "aa"}
  63. m := map[int]int{1: 1, 2: 2, 3: 3}
  64. f_1(a)
  65. f_2(s)
  66. f_3(v)
  67. f_4(m)
  68. fmt.Printf("%d,%s,%v,%v\n", a, s, v, m)
  69. f_3_1(v)
  70. f_4_1(m)
  71. fmt.Printf("%d,%s,%v,%v\n", a, s, v, m)
  72. f_1_1(&a)
  73. f_2_1(&s)
  74. f_3_2(&v)
  75. f_4_2(&m)
  76. fmt.Printf("%d,%s,%v,%v\n", a, s, v, m)
  77. b := []byte("12145178")
  78. f_5(b)
  79. fmt.Printf("%s\n", b)
  80. f_5_1(b)
  81. fmt.Printf("%s\n", b)
  82. f_5_2(&b)
  83. fmt.Printf("%s\n", b)
  84. ss := &why{}
  85. ss.SetV([]string{"abc", "efg"})
  86. fmt.Println(ss)
  87. ss.SetP([]string{"abc", "efg"})
  88. fmt.Println(ss)
  89. }

答案4

得分: 0

你可以这样写:

  1. a := "你好"
  2. b := "世界"
  3. a, b = b, a
英文:

you can write as that:

  1. a := "hello"
  2. b := "world"
  3. a, b = b, a

答案5

得分: 0

  1. package main
  2. import "fmt"
  3. func swap(a, b string) (string, string) {
  4. return b, a
  5. }
  6. func main() {
  7. fmt.Println(swap("Lang", "Go"))
  8. }
  1. package main
  2. import "fmt"
  3. func swap(a, b string) (string, string) {
  4. return b, a
  5. }
  6. func main() {
  7. fmt.Println(swap("Lang", "Go"))
  8. }
英文:
  1. package main
  2. import "fmt"
  3. func swap(a,b string)(string, string){
  4. return b,a
  5. }
  6. func main(){
  7. fmt.Println(swap("Lang","Go"))
  8. }

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:

确定