Skip to Content
初めてのGo言語 第2版 ―他言語プログラマーのためのイディオマティックGo実践ガイド
book

初めてのGo言語 第2版 ―他言語プログラマーのためのイディオマティックGo実践ガイド

by Jon Bodner, 武舎 広幸
August 2025
Beginner to intermediate
552 pages
7h 58m
Japanese
O'Reilly Japan, Inc.
Content preview from 初めてのGo言語 第2版 ―他言語プログラマーのためのイディオマティックGo実践ガイド

3章合成型

前の章では、リテラルと、事前宣言された型(数値型、論理値型、文字列型)について説明しました。この章では合成型(composite type)†1について見ていきます。また、合成型を扱う組み込みの関数や、それに関連する「ベストプラクティス」を紹介します。

[†1] 訳注:「コンポジット型」「複合型」「複合データ型」「コンテナ型」などとも呼ばれます。

[補足説明]

この章のファイル名が書かれていないコード例はch03/ex0300.goにあります。

3.1 配列

Go言語にも配列(array)がありますが、配列が直接使われることは多くはありません。その理由は後回しにして、まずは配列の宣言方法を見ましょう。

配列の各要素は指定された型でなければなりません†2。いくつかの宣言方法があります。最初の形式は、次のように配列の大きさと要素の型を指定するものです。

[†2] ただしこれは全要素が常に同じ型だということは意味しません。この点については「7.5 インタフェース」で説明します。

var x [3]int

この宣言で3個の整数を記憶する配列が生成されます。値が指定されていないので、すべての要素(x[0]x[1]x[2])は値としてintのゼロ値(すなわち0)をもちます。

宣言で値を指定する場合は、次のように配列リテラルを用います。

var x = [3]int{10, 20, 30}

途中に「空き」がある配列の値を指定したい場合は、配列のインデックス(そえ

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

データ保護完全ガイド ―あらゆるデータの保全と回復を可能にする

データ保護完全ガイド ―あらゆるデータの保全と回復を可能にする

W. Curtis Preston, 佐野 泰之, 池田 祥孝

Publisher Resources

ISBN: 9784814401192Publisher Website