在 Go 语言(通常称为 Golang)中,声明用于定义变量、常量、类型、函数、方法和接口。以下是一些常见的声明示例:
变量声明
var a int
var b, c float64
var d = 42 // 隐式类型推断
常量声明
const pi = 3.14159
类型声明
type Kilometers float64
函数声明
func add(x int, y int) int {
return x + y
}
方法声明
type Point struct {
X, Y float64
}
func (p *Point) Scale(factor float64) {
p.X *= factor
p.Y *= factor
}
接口声明
type Reader interface {
Read(p []byte) (n int, err error)
}
包声明
package main
导入包
import (
"fmt"
"net/http"
)
导入并重命名包
import (
"fmt"
"io" // 标准库中的 io 包
)
func main() {
fmt.Println("Hello, World!")
io.WriteString(os.Stdout, "Hello, World!\n")
}
导入匿名包
import _ "image/png" // 只用于它的副作用
这些是 Go 语言中一些基本的声明方式。如果你有更具体的问题或者需要关于特定声明的帮助,请告诉我!