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

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

by Jon Bodner, 武舎 広幸
September 2022
Beginner to intermediate
464 pages
6h 52m
Japanese
O'Reilly Japan, Inc.
Content preview from 初めてのGo言語 ―他言語プログラマーのためのイディオマティックGo実践ガイド

3章合成型

前の章では単純な型(数値、ブール値および文字列)について説明しました。この章では単純な型を使って作られる「合成型†1」について見ていきます。また、合成型を扱う組み込みの関数や、それに関連する「ベストプラクティス」を紹介します。

[†1] 「複合データ型」あるいは「コンテナ型」などとも呼ばれます。

[Tip]

この章のリスト番号の付いていないコード例はch03/ex0300.goにあります。

3.1 配列

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

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

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

var x [3]int

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

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

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

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

var x = [12]int{1, 5: 4,
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

進化的アーキテクチャ ―絶え間ない変化を支える

進化的アーキテクチャ ―絶え間ない変化を支える

Neal Ford, Rebecca Parsons, Patrick Kua, 島田 浩二
スクラム実践者が知るべき97のこと

スクラム実践者が知るべき97のこと

Gunther Verheyen, 吉羽 龍太郎, 原田 騎郎, 永瀬 美穂
仕事ではじめる機械学習 第2版

仕事ではじめる機械学習 第2版

有賀 康顕, 中山 心太, 西林 孝

Publisher Resources

ISBN: 9784814400041Other