Skip to content

Instantly share code, notes, and snippets.

@takumakei
Last active April 28, 2021 08:34
Show Gist options
  • Save takumakei/b265985f18ff5465a98cf9aedb8af7e2 to your computer and use it in GitHub Desktop.
Save takumakei/b265985f18ff5465a98cf9aedb8af7e2 to your computer and use it in GitHub Desktop.
Goの(&[5]int{})[:]の実験
package main
import "fmt"
func main() {
a := [5]int{}
b := a[:]
fmt.Println(b)
}
package main
import "fmt"
func main() {
a := (&[5]int{})[:]
fmt.Println(a)
}

Goの(&[5]int{})[:]の実験

実行例

$ make diff
cat -n 1.go
     1  package main
     2
     3  import "fmt"
     4
     5  func main() {
     6          a := [5]int{}
     7          b := a[:]
     8          fmt.Println(b)
     9  }
go build -trimpath -o 1 ./1.go
go tool objdump -gnu -s main.main 1 | sed 's/1.go//g' | cut -f2- > 1.dump
cat -n 2.go
     1  package main
     2
     3  import "fmt"
     4
     5  func main() {
     6          a := (&[5]int{})[:]
     7          fmt.Println(a)
     8  }
go build -trimpath -o 2 ./2.go
go tool objdump -gnu -s main.main 2 | sed 's/2.go//g' | cut -f2- > 2.dump
diff 1.dump 2.dump

関連

.DEFAULT_GOAL := help
.PHONY: help diff
help: ## show this message
@grep -E '^[a-zA-Z0-9_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-8s\033[0m %s\n", $$1, $$2}'
diff: ## 1 と 2 の go tool objdump の差分を表示する
diff: 1.dump 2.dump
diff $^
1.dump: 1
go tool objdump -gnu -s main.main 1 | sed 's/1.go/0.go/g' | cut -f2- > 1.dump
2.dump: 2
go tool objdump -gnu -s main.main 2 | sed 's/2.go/0.go/g' | cut -f2- > 2.dump
1:
cat -n 1.go
go build -trimpath -o 1 ./1.go
2:
cat -n 2.go
go build -trimpath -o 2 ./2.go
clean: ## clean
-rm 1 2 1.dump 2.dump
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment