错误:struct Type 不是一个表达式

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

Error: struct Type is not an expression

问题

使用struct和一个打印结构体元素的函数,我编写了这个简单的程序:

package main

import "fmt"

type Salutation struct {
    name     string
    greeting string
}

func Greet(salutation Salutation) {
    fmt.Println(salutation.name)
    fmt.Println(salutation.greeting)
}

func main() {
    var s = Salutation
    s.name = "Alex"
    s.greeting = "Hi"
    Greet(s)
}

当我运行它时,出现错误go:16: type Salutation is not an expression。这里出了什么问题?

有趣的是,当我将s的定义更改为var s = Salutation{"Alex", "Hi"}时,它可以正常工作。但它们基本上是定义相同实体的不同语法方式。这就是我不理解错误来源的原因。

英文:

Using struct and a function that is supposed to print out the struct's elements, I have written this simple program:

package main

import "fmt"

type Salutation struct {
	name     string
	greeting string
}

func Greet(salutation Salutation) {
	fmt.Println(salutation.name)
	fmt.Println(salutation.greeting)
}

func main() {
	var s = Salutation
	s.name = "Alex"
	s.greeting = "Hi"
	Greet(s)
}

When I run it I get the error go:16: type Salutation is not an expression. What goes wrong here?

Interestingly enough, when I change the definition of s to var s = Salutation {"Alex", "Hi"} it works just fine. But they are basically different syntactic ways to define the same entity. That's why I don't understand the source of the error.

答案1

得分: 90

错误出现在这一行:

    var s = Salutation

等号右边的内容必须求值为一个值。Salutation 是一个类型,不是一个值。以下是三种声明变量 s 的方式:

 var s Salutation      // 使用类型进行变量声明

 var s = Salutation{}  // 使用值进行变量声明

 s := Salutation{}     // 简短变量声明

这三种声明的结果是相同的。通常情况下,第三种方式比第二种方式更受欢迎,但不能用于声明包级别的变量。

有关变量声明的详细信息,请参阅语言规范 Variable declarations

变量声明和字段初始化可以合并为一条语句:

 var s = Salutation{name: "Alex", greeting: "Hello"} // 变量声明

 s := Salutation{name: "Alex", greeting: "Hello"}    // 简短变量声明
英文:

The error is on this line

    var s = Salutation

The thing to the right of the = must evaluate to a value. Salutation is a type, not value. Here are three ways to declare s:

 var s Salutation      // variable declaration using a type 

 var s = Salutation{}  // variable declaration using a value

 s := Salutation{}     // short variable declaration

The result of all three declarations is identical. The third variation is usually preferred to the second, but cannot be used to declare a package-level variable.

See the language specification for all of the details on variable declarations.

The variable declaration and field initializations can be combined into a single statement:

 var s = Salutation{name: "Alex", greeting: "Hello"} // variable declaration

 s := Salutation{name: "Alex", greeting: "Hello"}    // short variable declaration

答案2

得分: 6

第四种方法:

*var s Salutation = &( Salutation{} );

我总是通过引用传递结构体,而不是值。
并且总是通过值传递基本类型。

将你的方法重写为接收器方法:

func (s *Salutation) Greet()() {
    fmt.Println(s.name)
    fmt.Println(s.greeting)
}

完整示例:

package main

import "fmt"

func NewSalutation()(*Salutation){
    return &( Salutation{} );
}
type Salutation struct {
    name     string
    greeting string
}

func (s *Salutation) Greet()() {
    fmt.Println(s.name)
    fmt.Println(s.greeting)
}

func main() {
    var s *Salutation;   //:<--Null
    s = NewSalutation()  //:<--Points To Instance
    s.name     = "Alex"
    s.greeting = "Hi"
    s.Greet();
}
英文:

4th way:

*var s Salutation = &( Salutation{} );

I always pass structs by reference, not value.
And always pass primitives by value.

Your method re-written as a reciever method:

func (s *Salutation) Greet()() {
    fmt.Println(s.name)
    fmt.Println(s.greeting)
}

Full Example:

package main

import "fmt"

func NewSalutation()(*Salutation){
	return &( Salutation{} );
}
type Salutation struct {
    name     string
    greeting string
}

func (s *Salutation) Greet()() {
    fmt.Println(s.name)
    fmt.Println(s.greeting)
}

func main() {
    var s *Salutation;   //:<--Null
	s = NewSalutation()  //:<--Points To Instance
    s.name     = "Alex"
    s.greeting = "Hi"
    s.Greet();
}

huangapple
  • 本文由 发表于 2014年12月13日 11:13:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/27455170.html
匿名

发表评论

匿名网友

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

确定