問題
package main
import (
  "fmt"
)
func main() {
  var word string = 'ABC'
  fmt.Println(word)
}
// # command-line-arguments
// ./single_quotes.go:8: missing '
// ./single_quotes.go:8: syntax error: unexpected BC at end of statement
// ./single_quotes.go:8: newline in character literal
理由
go でのシングルクオートは、Rune という 型を扱うらしい。
何か文字を渡すと int32 で Unicode Code Point にマッピングされるようだ。
package main
import (
  "fmt"
)
func main() {
  var english_a rune = 'a'
  fmt.Println(english_a) // 97
  fmt.Println('b') // 98
  fmt.Println('あ') // 12354
  fmt.Println('い') // 12346
}
解決
文字列はダブルクオートで囲おう。
package main
import (
  "fmt"
)
func main() {
  var word string = "ABC"
  fmt.Println(word) // ABC
}
参考
- http://stackoverflow.com/questions/34691045/cannot-assign-string-with-single-quote-in-golang
 - http://stackoverflow.com/questions/19310700/what-is-a-rune
 
チャットメンバー募集
何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。