LoginSignup
0
0

More than 3 years have passed since last update.

Go言語のA Tour of Goの(公式チュートリアル)の学習メモ

Posted at

A Tour of Goでは日本語訳もありますが、英語の学習もついでにやりたいと思って英語でチュートリアルをやりました。この記事はその際の学習メモです。

Packages, variables and functions.

Packages

Every Go program is made up of packages.

全てのGoのプログラムはパッケージによって構成されています。

Programs start running in package main.

プログラムはmainパッケージで開始されます。

By convention, the package name is the same as the last element of the import path. For instance, the "math/rand" package comprises files that begin with the statement package rand.

慣習的にはパッケージ名はインポートパスの最後の要素と同一になります。

Imports

This code groups the imports into a parenthesized, "factored" import statement.

この「因数分解された」インポートステートメントはインポートを括弧でひとまとめにします。

You can also write multiple import statements(...)
But it is good style to use the factored import statement.

同様に複数のインポートステートメントを記述することもできますが、「因数分解」ステートメントのスタイルを用いるのが良いでしょう。

Exported names

In Go, a name is exported if it begins with a capital letter.

Goではnameが大文字から始まる場合、それは外部にエキスポートされます。

When importing a package, you can refer only to its exported names. Any "unexported" names are not accessible from outside the package.

パッケージをインポートする際、あなたはエキスポートされたnameのみを参照することができます。
エキスポートされていないnameはパッケージの外部からはアクセス不可能です。

Functions

A function can take zero or more arguments.

関数は0個あるいはそれ以上の引数を取得することができます。

Notice that the type comes after the variable name.

型が変数名の後になっていることに注意してください。

(For more about why types look the way they do, see the article on Go's declaration syntax.)

なぜこのようになっているかについては、Goの定義文についての記事を参照してください。

Functions continued

When two or more consecutive named function parameters share a type, you can omit the type from all but the last.

二つあるいはそれ以上の連続した名前付きパラメータが型を共有している場合、最後以外の型を省略することができます。

Multiple results

A function can return any number of results.

関数は幾つの返り値でも返すことができます。

Named return values

Go's return values may be named. If so, they are treated as variables defined at the top of the function.

Goの返り値は名前が付けられる場合があります。そしてそうなった場合、それらは関数の冒頭で変数定義として扱われます。

These names should be used to document the meaning of the return values.

それらの名前は返り値の意味合いをドキュメント化しておくべきです。

A return statement without arguments returns the named return values. This is known as a "naked" return.

引数のな無いreturnステートメントは、名前付き返り値を返却します。これは「むき出しの」returnとして知られています。

Naked return statements should be used only in short functions(...). They can harm readability in longer functions.

naked returnステートメントは短文の関数の中でのみ使われるべきです。これらは長文の関数の中では可読性を阻害します。

Variables

The var statement declares a list of variables; as in function argument lists, the type is last.

varステートメントは変数リストを宣言します。関数の引数リストのように、型は最後に記述します。

A var statement can be at package or function level.

varステートメントはパッケージレベルあるいは関数レベルで可能です。

Variables with initializers

A var declaration can include initializers, one per variable.

変数宣言は変数ごとに一つずつ、イニシャライザを含むことができます。

If an initializer is present, the type can be omitted; the variable will take the type of the initializer.

イニシャライザが与えられた場合、型は省略することができます。変数はイニシャライザの型を取得します。

Short variable declarations

Inside a function, the := short assignment statement can be used in place of a var declaration with implicit type.

関数内部では、:=短縮指定ステートメントは暗黙の型とともにvar宣言の部分で利用することができます。

Outside a function, every statement begins with a keyword (var, func, and so on) and so the := construct is not available.

関数外では全てのステートメントはキーワード(var, funcなど)とともに始まります、そのため:=は使用することができません。

Basic types

Go's basic types are

bool

string


int  int8  int16  int32  int64
uint uint8 uint16 uint32 uint64 uintptr

byte // alias for uint8

rune // alias for int32
     // represents a Unicode code point

float32 float64

complex64 complex128

The int, uint, and uintptr types are usually 32 bits wide on 32-bit systems and 64 bits wide on 64-bit systems. When you need an integer value you should use int unless you have a specific reason to use a sized or unsigned integer type.

int型, unint型, unintprt型は一般的に32ビットのシステムでは32ビットの幅を、64ビットのシステムでは64ビットの幅を持っています。あなたが整数型の値を必要とするときは、特定の制限または符号無し整数型を利用する特定の理由がない限りintを使用するべきです。

Zero values

Variables declared without an explicit initial value are given their zero value.

明示的な初期値なく宣言された変数はゼロ値を与えられます。

The zero value is:

0 for numeric types,
false for the boolean type, and
"" (the empty string) for strings.

ゼロ値は数値型では0、論理型ではfalse、文字列型では""(空文字)です。

Type conversions

The expression T(v) converts the value v to the type T.

T(v)は値vを型Tへ変換します。

Some numeric conversions:

いくつかの数値の変換:

var i int = 42
var f float64 = float64(i)
var u uint = uint(f)

Or, put more simply:

あるいはより単純に、

i := 42
f := float64(i)
u := uint(f)

Type inference

When declaring a variable without specifying an explicit type (either by using the := syntax or var = expression syntax), the variable's type is inferred from the value on the right hand side.

変数宣言時に明示的な型が指定されなかった場合(:=構文とvar =式のいずれでも)、変数の肩は右側の値から推論されます。

When the right hand side of the declaration is typed, the new variable is of that same type:

右側の宣言が型宣言されている場合、新規の変数は同一の型になります。

var i int
j := i // j is an int

But when the right hand side contains an untyped numeric constant, the new variable may be an int, float64, or complex128 depending on the precision of the constant:

しかし右側が型宣言されていない数値の定数だった場合、新規変数は定数の精度によってintfloat64、あるいはcomplex128になるかもしれません。

i := 42           // int
f := 3.142        // float64
g := 0.867 + 0.5i // complex128

Constants

Constants are declared like variables, but with the const keyword.

定数は変数のように宣言されますが、constキーワードを伴います。

Constants cannot be declared using the := syntax.

定数は:-構文を使って宣言することはできません。

Numeric Constants

Numeric constants are high-precision values.

数値定数は高精度の値です。

An untyped constant takes the type needed by its context.

不明な型の定数は文脈によって型を取得します。

参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0