Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

Go | シングルクオートでは文字列を定義できない

More than 3 years have passed since last update.

問題

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
}

参考

YumaInaura
Ruby on Rails 業務経験 約5年 / Perl PHP Python Golang Linux Apache MySQL BigQuery Jenkins ansible AWS など / いなうらゆうま / YumaInaura / 稲浦悠馬
http://twitter.com/yumainaura
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away