ebook img

GoLang: The Ultimate Guide PDF

366 Pages·2022·7.104 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview GoLang: The Ultimate Guide

GoLang Go, also known as GoLang, is a Google-developed open-source, compiled, and statically typed computer language. Go is a general purpose programming lan- guage with a straightforward syntax and a large standard library. The building of highly accessible and scalable web apps is one of the primary areas where GoLang is widely used. It may also be used to develop command-line programs, desktop applications, and even mobile apps. Go was designed from the ground up for networking and infrastructure-related applications. It was developed as a replacement for popular server-side languages like Java and C++. The Go programming language aims to combine the efficiency and safety of a statically typed, compiled language with the simplicity of program- ming of an interpreted, dynamically typed language. It also aspires to be cutting- edge, with networked and multicore computer capabilities. Why Should You Learn GoLang? GoLang is becoming one of the most popular languages, which means that learning it can open up new doors of opportunity and even help you land a job at various companies that use Go extensively. Ease of writing concurrent programs, fast compilation, simple syntax, and static linking are some of the features that make Go an ideal candidate for developing various applications. Features: • Step-by-step approach to problem solving and skill development • A quick run-through of the basic concepts, in the form of a “Crash Course” • Advanced, hands-on core concepts, with a focus on real-world problems • Industry-level coding paradigm, practice-oriented explanatory approach • Special emphasis on writing clean and optimized code, with addi- tional chapters focused on coding methodology GoLang The Ultimate Guide Edited by Sufyan bin Uzayr First edition published 2023 by CRC Press 6000 Broken Sound Parkway NW, Suite 300, Boca Raton, FL 33487-2742 and by CRC Press 4 Park Square, Milton Park, Abingdon, Oxon, OX14 4RN CRC Press is an imprint of Taylor & Francis Group, LLC © 2023 Sufyan bin Uzayr Reasonable efforts have been made to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future reprint. Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, microfilming, and recording, or in any information storage or retrieval system, without written permission from the publishers. For permission to photocopy or use material electronically from this work, access www.copyright.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. For works that are not available on CCC please contact mpkbookspermissions@tandf. co.uk Trademark Notice: Product or corporate names may be trademarks or registered trademarks and are used only for identification and explanation without intent to infringe. Library of Congress Cataloging-in-Publication Data Names: Bin Uzayr, Sufyan, author. Title: GoLang : the ultimate guide / Sufyan bin Uzayr. Description: First edition. | Boca Raton : CRC Press, 2023. | Includes bibliographical references and index. Identifiers: LCCN 2022025681 (print) | LCCN 2022025682 (ebook) | ISBN 9781032312323 (hardback) | ISBN 9781032312316 (paperback) | ISBN 9781003309055 (ebook) Subjects: LCSH: Go (Computer program language) Classification: LCC QA76.73.G63 B56 2023 (print) | LCC QA76.73.G63 (ebook) | DDC 005.13/3--dc23/eng/20220919 LC record available at https://lccn.loc.gov/2022025681 LC ebook record available at https://lccn.loc.gov/2022025682 ISBN: 9781032312323 (hbk) ISBN: 9781032312316 (pbk) ISBN: 9781003309055 (ebk) DOI: 10.1201/9781003309055 Typeset in Minion by KnowledgeWorks Global Ltd. Contents Acknowledgments, xix About the Author, xxi Chapter 1 ◾ C rash Course in GoLang 1 IS IT REFERRED TO AS Go OR GoLang? 2 WHAT IS THE PURPOSE OF GoLang? 2 WHY IS Go SO POPULAR? 2 Other Programming Languages vs. Go 3 Python vs. Go 3 But How Does Go Stack Up against Python? 3 SHOULD WE STUDY Go? 4 Go PROGRAMMING FEATURES 4 WHAT MAKES GoLang SUPERIOR TO OTHER COMPUTER LANGUAGES? 5 GoLang’s Core Capability 5 Multithreading and Concurrency 6 From within, Go Empowers Hardware 6 Unmatched Simplicity of Go 7 Built-in Testing and Profiling Framework 7 Easy Learning Curve 7 BEGINNING WITH Go 8 Text Editor 8 Finding a Go Compiler 8 v vi ◾ Contents INSTALL Go ON WINDOWS 8 How Can We Know Which Go Language Version Is Preinstalled? 9 Downloading and Installing Go 9 WRITING THE FIRST Go PROGRAM 10 Why Is There a “Go Language”? 11 What Is Missing in Go That the Other Languages Have? 11 Hardware Restrictions 12 The Upsides and Downsides of the Go Language 12 TERMINAL 13 The Open Terminal Tool Window 13 Start the New Session 14 INSTALL Go ON Mac 14 Making Our First Program 16 Implement a Go Program 16 Do Go Programs Interact with Programs Written in C/C++? 17 HOW CAN WE CREATE AN EMPTY FILE IN GoLang? 17 HOW TO MAKE A DIRECTORY IN Go 19 HOW TO READ AND WRITE A Go PROGRAM 20 HOW DO WE RENAME AND TRANSFER A FILE IN GoLang? 23 HOW TO READ A FILE LINE BY LINE AND CONVERT IT TO A STRING 24 BASIC SYNTAX 25 Tokens 25 Line Separator 25 Comments 25 Identifiers 26 Keywords 26 Whitespace 27 DATA TYPES IN Go 27 Numbers 28 Contents ◾ vii Booleans 30 Strings 30 VARIABLES IN Go 31 Declaring a Variable 32 Employing the var Keyword 32 Using the Short Variable Declaration 35 CONSTANTS 38 How Should We Declare? 39 Untyped and Typed Numeric Constants 39 Numeric Constant 39 String Literals 41 Boolean Constant 42 VARIABLE SCOPE IN Go 43 Local Variables 43 Global Variables 44 DECLARATION OF MULTIPLE VARIABLES 46 SHORTHAND DECLARATION 48 OPERATORS IN Go 49 Arithmetic Operators 50 Relational Operators 51 Logical Operators 53 Bitwise Operators 53 Assignment Operators 54 Misc Operators 56 CONTROL STATEMENTS 57 if Statement 57 if…else Statement 58 Nested if Statement 60 if..else..if Ladder 61 SWITCH STATEMENT 63 Expression Switch 63 Type Switch 64 viii ◾ Contents ARRAYS 65 Creating and Using an Array 65 Using var Keyword 65 Using a Shorthand Declaration 66 Multidimensional Array 67 How Do You Copy an Array into Another Array? 68 How Can We Pass an Array to a Function? 69 SLICES 70 Slice Declaration 70 Slice Components 71 How Do We Create and Begin a Slice? 72 Using Slice Literal 72 Using an Array 72 Using an Existing Slice 73 Using the make() Function 74 How to Iterate over a Slice 75 Using the for Loop 75 Using Range in a for Loop 76 Using a Blank Identifier in a for Loop 76 Slice Composite Literal 77 How to Sort a Slice of ints 77 Ints 78 IntsAreSorted 78 How Do We Trim a Slice of Bytes in GoLang? 79 How Do We Split a Slice of Bytes? 81 STRING 82 String Literals 83 Using Double Quotes (“”) 83 Using Backticks(“) 83 How Do We Trim a String? 84 Trim 84 TrimLeft 85 Contents ◾ ix TrimRight 86 TrimSpace 87 TrimSuffix 88 TrimPrefix 89 How to Split a String 90 Split 90 SplitAfter 91 SplitAfterN 92 MAPS 93 How Do We Create and Initialize Maps? 94 Simple 94 Using the make() Function 95 WHAT IS A BLANK IDENTIFIER (underscore)? 96 DEFER KEYWORD 97 PANIC IN GoLang 98 RECOVER 99 CLOSURES 100 RECURSION 101 POINTERS 102 What Are Points Used For? 102 Pointers Declaration and Initialization 103 Declaring a Pointer 103 Pointer Initialization 103 Dereferencing a Pointer 104 In GoLang, How Can We Instantiate a Struct Using the New Keyword? 105 Pointers to a Function 106 Create a Pointer and Pass It to the Function 106 Passing an Address of the Variable to Function Call 107 Pointer to a Struct 108 Pointer to Pointer in Go 109 How to Declare a Pointer to a Pointer 109

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.