2015-11-15 17:29
浏览 1.5k


Aim: understanding the difference between *string and string in Golang


func passArguments() {
    username := flag.String("user", "root", "Username for this server")
    fmt.Printf("Your username is %q.", *username)
    fmt.Printf("Your username is %q.", username)

results in:

Your username is "root".Your username is %!q(*string=0xc820072200)

but when the *string is assigned to a string:

fmt.Printf("Your username is %q.", bla)

it is able to print the string again:

Your username is "root".Your username is %!q(*string=0xc8200781f0).Your username is "root".


  1. Why is a *string != string, e.g. display of: "root" vs. %!q(*string=0xc8200781f0)?
  2. In what other cases should a *string be used instead of a string and why?
  3. Why is it possible to assign a *string to a string variable, while the display of the string is different, e.g. display of: "root" vs. %!q(*string=0xc8200781f0)?

图片转代码服务由CSDN问答提供 功能建议

目标:了解 * string 和< Golang中的code> string


  func  passArguments(){
用户名:= flag.String(“ user”,“ root”,“此服务器的用户名”)
 fmt.Printf(“您的用户名是%q。”,  * username)

导致: \ n

 您的用户名是“ root”。您的用户名是%!q(* string = 0xc820072200)

,但是指定了* string 到字符串:

  bla:= *用户名
   \  n 


 您的用户名是“ root”。您的用户名是%!q(* string = 0xc8200781f0)。您的 用户名是“ root”。


    \ n
  1. 为什么是* string!=字符串,例如 显示:“ root” vs. %!q(* string = 0xc8200781f0)
  2. 在其他什么情况下,应该使用* string代替字符串,为什么?
  3. 为什么可以在字符串显示不同的情况下将 * string分配给字符串变量,例如 显示以下内容:“ root” vs. %!q(* string = 0xc8200781f0)
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongping2023 2015-11-15 17:58

    A *string is a pointer to a string. If you're not familiar with pointers, let's just say that it's a value that holds the address of another value, instead of the value itself (it's a level of indirection).

    When a * is used in a type, it denotes a pointer to that type. *int is a pointer to an integer. ***bool is a pointer to a pointer to a pointer to a bool.

    flag.String returns a pointer to a string because it it can then modify the string value (after the call to flag.Parse) and you are able to retrieve that value using the dereference operator - that is, when using * on a variable, it dereferences it, or retrieves the value pointed to instead of the value of the variable itself (which in the case of a pointer would just be a memory address).

    So to answer your specific questions:

    1. the %q verb in the fmt package understands strings (and slices of bytes), not pointers, hence the apparent gibberish displayed (when a value is not of the expected type for the matching verb - here %q - the fmt functions display %!q along with the actual type and value passed)

    2. A pointer to a string is very rarely used. A string in Go is immutable (https://golang.org/ref/spec#String_types) so in cases like flag.String where you need to return a string that will be mutated later on, you have to return a pointer to a string. But you won't see that very often in idiomatic Go.

    3. You are not assigning a *string (pointer to a string) to a string. What you are doing, as I mentioned earlier, is dereferencing the *string variable, extracting its string value. So you are in fact assigning a string to a string. Try removing the * on that line, you'll see the compiler error message. (actually, because you're using the short variable declaration notation, :=, you won't see a compiler error, but your variable will be declared as a pointer-to-a-string. Try this instead, to better understand what's going on:

      var s string
      s = username

    That will raise the compiler error).

    解决 无用
    打赏 举报

相关推荐 更多相似问题