2013-05-16 01:27 阅读 92


I'm following this tutorial, specifically exercise 8:

package main

import "fmt"

func swap(x, y string) (string, string) {
    return y, x

func main() {
    a, b := swap("hello", "world")
    fmt.Println(a, b)

Specifically what does the := mean? Searching for Go documentation is very hard, ironically.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

5条回答 默认 最新

  • 已采纳
    doumanju2533 doumanju2533 2013-05-16 01:30

    A short variable declaration uses the syntax:

    ShortVarDecl = IdentifierList ":=" ExpressionList .

    It is a shorthand for a regular variable declaration with initializer expressions but no types:

    点赞 评论 复制链接分享
  • duandai2178 duandai2178 2013-05-16 01:47

    Keep on going to page 12 of the tour!

    A Tour of Go

    Short variable declarations

    Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.

    (Outside a function, every construct begins with a keyword and the := construct is not available.)

    点赞 评论 复制链接分享
  • dts777777 dts777777 2015-11-23 08:39

    := represents a variable, we can assign a value to a variable using :=.

    点赞 评论 复制链接分享
  • douqian5553 douqian5553 2015-11-25 14:53

    According to my book on Go, it is just a short variable declaration statement exactly the same as

    var s = ""

    But it is more easy to declare, and the scope of it is less expansive. A := var decleration also can't have a type of interface{}. This is something that you will probably run into years later though

    点赞 评论 复制链接分享
  • duanfan1965 duanfan1965 2018-11-13 13:21

    := is not an operator. It is a part of the syntax of the Short variable declarations clause.

    more on this:

    点赞 评论 复制链接分享