1. Trang chủ
  2. » Công Nghệ Thông Tin

Programming in Go: Creating Applications for the 21st Century, Rough Cuts docx

493 1,2K 1

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 493
Dung lượng 7,44 MB

Nội dung

ptg7913109 www.it-ebooks.info ptg7913109 Programming in Go www.it-ebooks.info ptg7913109 T he Developer’s Library Series from Addison-Wesley provides practicing programmers with unique, high-quality references and tutorials on the latest programming languages and technologies they use in their daily work. All books in the Developer’s Library are written by expert technology practitioners who are exceptionally skilled at organizing and presenting information in a way that’s useful for other programmers. Developer’s Library books cover a wide range of topics, from open- source programming languages and databases, Linux programming, Microsoft, and Java, to Web development, social networking platforms, Mac/iPhone programming, and Android programming. Visit developers-library.com for a complete list of available products Developer’s Library Series www.it-ebooks.info ptg7913109 Programming in Go Creating Applications for the 21st Century Mark Summerfield Upper Saddle River, NJ · Boston · Indianapolis · San Francisco p New York · Toronto · Montreal · London · Munich · Paris · Madrid p Capetown · Sydney · Tokyo · Singapore · Mexico City www.it-ebooks.info ptg7913109 Many of the designations used by manufacturersand sellers to distinguish their productsare claimed as trademarks. Where those designations appear in this book, and the publisher was aware of a trademark claim, the designations have been printed with initial capital letters or in all capitals. The author and publisher have taken care in the preparation of this book, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for incidental or consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk purchases or special sales, which may include electronic versions and/or custom covers and content particular to your business, training goals, marketing focus, and branding interests. For more information, please contact: U.S. Corporate and Government Sales (800) 382-3419 corpsales@pearsontechgroup.com For sales outside the United States, please contact: International Sales international@pearsoned.com Visit us on the Web: informit.com/aw Library of Congress Cataloging-in-Publication Data Summerfield, Mark. Programming in Go : creating applications for the 21st century / Mark Summerfield. p.mcm. Includes bibliographical references and index. ISBN 978-0-321-77463-7 (pbk. : alk. paper) 1. Go (Computer program language) 2. Computer programming 3. Application software— Development I. Title. QA76.73.G63S86 2012 005.13 ’ 3—dc23 2012001914 Copyright © 2012 Qtrac Ltd. All rights reserved. Printed in the United States of America. This publication is protected by copyright, and permission must be obtained from the publisher prior to any prohibited reproduction, storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical, photocopying, recording, or likewise. To obtain permission to use material from this work, please submit a written request to Pearson Education, Inc., Permissions Department, One Lake Street, Upper Saddle River, New Jersey 07458, or you may fax your request to (201) 236-3290. ISBN-13: 978-0-321-77463-7 ISBN-10: 0-321-77463-9 Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana. First printing, April 2012 www.it-ebooks.info ptg7913109 This book is dedicated to Jasmin Blanchette and Trenton Schulz www.it-ebooks.info ptg7913109 This page intentionally left blank www.it-ebooks.info ptg7913109 Contents at a Glance Tables xv Introduction 1 Chapter 1. An Overview in Five Examples 7 Chapter 2. Booleans and Numbers 51 Chapter 3. Strings 81 Chapter 4. Collection Types 139 Chapter 5. Procedural Programming 185 Chapter 6. Object-Oriented Programming 253 Chapter 7. Concurrent Programming 315 Chapter 8. File Handling 361 Chapter 9. Packages 407 Appendix A. Epilogue 435 Appendix B. The Dangers of Software Patents 437 Appendix C. Selected Bibliography 441 Index 443 www.qtrac.eu/gobook.html www.it-ebooks.info ptg7913109 This page intentionally left blank www.it-ebooks.info ptg7913109 Contents Tables xv Introduction 1 Why Go? 1 The Structure of the Book 4 Acknowledgments 5 Chapter 1 . An Overview in Five Examples 7 1.1. Getting Going 7 1.2. Editing, Compiling, and Running 9 1.3. Hello Who? 14 1.4. Big Digits—Two-Dimensional Slices 16 1.5. Stack—Custom Types with Methods 21 1.6. Americanise—Files, Maps, and Closures 29 1.7. Polar to Cartesian—Concurrency 40 1.8. Exercise 48 Chapter 2 . Booleans and Numbers 51 2.1. Preliminaries 51 2.1.1. Constants and Variables 53 2.1.1.1. Enumerations 54 2.2. Boolean Values and Expressions 56 2.3. Numeric Types 57 2.3.1. Integer Types 59 2.3.1.1. Big Integers 61 2.3.2. Floating-Point Types 64 2.3.2.1. Complex Types 70 2.4. Example: Statistics 72 2.4.1. Implementing Simple Statistics Functions 73 2.4.2. Implementing a Basic HTTP Server 75 2.5. Exercises 78 ix www.it-ebooks.info [...]... toolchain includes programs such as 5g, 6g, and 8g for compiling, 5l, 6l, and 8l for linking, and godoc for viewing the Go documentation (These are 5g.exe, 6l.exe, etc., on Windows.) The strange names follow the Plan 9 operating system’s compiler naming conventions where the digit identifies the processor architecture (e.g., “5” for ARM, “6” for AMD64—including Intel 64-bit processors—and “8” for Intel... is also possible to view the documentation for a whole package or a single item in a package in the console using godoc on the command line For example, executing godoc image NewRGBA will output the documentation for the image.NewRGBA() function, and executing godoc image/png will output the documentation for the entire image/png package www.it-ebooks.info 1.1 Getting Going 9 The standard Go compiler... provides functions for formatting text and for reading formatted text (§3.5, ➤ 93) The log package provides logging functions The os package provides platform-independent operating-system variables and functions including the os.Args variable of type []string (slice of strings) that holds the command-line arguments And the path package’s filepath package provides functions for manipulating filenames and... valid for the entire 1.x series (If incompatible changes occur, the book’s examples will be updated to the latest Go release, so as time goes by, they may differ from the code shown in the book.) To download and install Go, visit golang.org/doc/install.html which provides instructions and download links At the time of this writing, Go 1 is available in source and binary form for FreeBSD 7+, Linux 2.6+,... any Go program into a shebang script This is done with two simple steps First, add either #!/usr/bin/env gonow or #!/usr/bin/env gorun, as the very first line of the go file that contains the main() function (in package main) Second, make the file executable (e.g., with chmod +x) Such files can only be compiled by gonow or gorun rather than in the normal way since the #! line is not legal in Go When gonow... variable, GOROOT, which contains the path to the Go installation, and your PATH should now include $GOROOT/bin or %GOROOT%\bin To check that Go is installed www.it-ebooks.info 1.2 Editing, Compiling, and Running 11 correctly, enter the following in a console (xterm, gnome-terminal, konsole, Terminal.app, or similar): $ go version Or on Windows in an MS-DOS Prompt or Command Prompt window: C:\>go version If... System, then Advanced system settings, and in the System Properties dialog click the Environment Variables button, then the New… button, and add a variable with the name GOROOT and a suitable value, such as C:\Go In the same dialog, edit the PATH environment variable by adding the text ;C:\Go\bin at the end the leading semicolon is vital! In both cases replace the C:\Go path component with the actual... neatly modularized, something we will see in Chapter 9 The import statement (14 ➤, ➊) imports three packages from the standard library The fmt package provides functions for formatting text and for reading formatted text (§3.5, ➤ 93), the os package provides platform-independent operating-system variables and functions, and the strings package provides functions for manipulating strings (§3.6.1, ➤ 107)... using the [] index operator using a subset of the Python syntax In particular, slice[n] returns the slice’s nth element (counting from zero), and slice[n:] returns another slice which has the elements from the nth element to the last element In the collections chapter we will see the full generality of Go’s syntax in this area In the case of os.Args, the slice should always have at least one string... string (the program’s name), at index position 0 (All Go indexing is 0-based.) If the user has entered one or more command line arguments the if condition is satisfied and we set the who string to contain all the arguments joined up as a single string (14 ➤, ➍) In this case we use the assignment operator (=), since if we used the short variable declaration operator (:=) we would end up declaring and initializing . Formatting with the Fmt Package 93 3.5.1. Formatting Booleans 97 3.5.2. Formatting Integers 98 3.5.3. Formatting Characters 99 3.5.4. Formatting Floating-Point Numbers 100 3.5.5. Formatting Strings. consequential damages in connection with or arising out of the use of the information or programs contained herein. The publisher offers excellent discounts on this book when ordered in quantity for bulk. Cataloging -in- Publication Data Summerfield, Mark. Programming in Go : creating applications for the 21st century / Mark Summerfield. p.mcm. Includes bibliographical references and index. ISBN 978-0-321-77463-7

Ngày đăng: 29/03/2014, 22:20

TỪ KHÓA LIÊN QUAN