Serie: Go
go
41 líneas
· Actualizado 2026-04-18
main.go
Go/part1-language/examples/08_pointers/main.go
// Demonstrates pointers, nil safety, value vs pointer semantics.
package main
import "fmt"
type Big struct {
Data [1 << 10]byte
Tag string
}
func setTagByValue(b Big, t string) { b.Tag = t }
func setTagByPointer(b *Big, t string) { b.Tag = t }
func newCounter() *int {
n := 0
return &n
}
func main() {
x := 10
p := &x
*p = 99
fmt.Println("x =", x)
var np *int
fmt.Println("nil ptr:", np == nil)
b := Big{Tag: "init"}
setTagByValue(b, "by value")
fmt.Println("after by value:", b.Tag)
setTagByPointer(&b, "by pointer")
fmt.Println("after by pointer:", b.Tag)
c := newCounter()
*c++
*c++
fmt.Println("counter:", *c)
pi := new(int)
fmt.Println("new int:", *pi)
}
Artículos relacionados
Go
go
Actualizado 2026-04-18
main.go
main.go — go source code from the Go learning materials (Go/Baasid/cmd/hashpw/main.go).
Leer artículo →
Go
go
Actualizado 2026-04-18
main.go
main.go — go source code from the Go learning materials (Go/Baasid/cmd/server/main.go).
Leer artículo →
Go
go
Actualizado 2026-04-18
docs.go
docs.go — go source code from the Go learning materials (Go/Baasid/docs/docs.go).
Leer artículo →
Go
go
Actualizado 2026-04-18
jwt.go
jwt.go — go source code from the Go learning materials (Go/Baasid/internal/auth/jwt.go).
Leer artículo →
Go
go
Actualizado 2026-04-18
password.go
password.go — go source code from the Go learning materials (Go/Baasid/internal/auth/password.go).
Leer artículo →
Go
go
Actualizado 2026-04-18
password_test.go
password_test.go — go source code from the Go learning materials (Go/Baasid/internal/auth/password_test.go).
Leer artículo →