Table of Contents....5
About the Author....12
About the Technical Reviewer....13
Acknowledgments....14
Introduction....15
Assessments....30
Part I: First Look at Go....31
Chapter 1: A Brief Look at Go vs. Java....34
Go Is a Compiled (vs. Interpreted, As Java Is) Language....35
Go and Java Share Similar Program Structure....36
Go and Java Have Some Code Style Differences That Influence How Code Is Structured....36
Both Go and Java Are Procedural Languages....37
Java Is an Object-Oriented (OO) Language, While Go Is Not Fully OO....38
Java Is a Highly Functional Language, Go Is Less So....39
Java Is a Highly Declarative Language, Go Is Less So....39
Many Java Features Are Annotation Driven....39
Go Does Not Support Exceptions....40
Both Java and Go Use Managed Memory (Garbage Collectors)....40
Go and Java Both Support Concurrency but in Different Ways....41
Go’s Runtime Is Simpler Than the JRE....41
The Go Program Build Process Is Different....42
Go and Java Have Similar Release Cycles....42
Chapter 2: What Java Has That Go Does Not....44
Multiple Assignments....44
Statements and Operators....44
Assert Statement....45
While and Do Statements....45
Throw Statement/Throws Clause....46
Strictfp, transient, volatile, synchronized, abstract, static....46
Objects and Classes (OOP) and Inner Classes, Lambdas, this, super, Explicit Constructors....46
Generic Types and Methods....49
Extensive Functional Programming Capabilities....49
Boxing of Primitive Values....50
Source Annotations....50
Multiple Visibilities....51
Overloaded/Overridden Functions....51
Formal Enums....51
Built-in Binary Data Self-Serialization....52
Concurrent Collections....52
Chapter 3: A Deeper Comparison of Go and Java....53
Part II: The Go Language....58
Chapter 4: Key Go Aspects....59
Simple Go Program Example....61
Go Packages....67
Go Comments....70
Go Build/Run Process....71
Go Playground....72
Go Integrated Development Environments....76
Running Go Programs....81
Building Go Programs....82
Bytecode vs. Real Code....85
Go Command-Line Tools....87
Tools Bundled in the Go Command....88
Other Tools....90
Go Runs Programs Instead of Classes....90
Go Memory Management....92
Go Identifiers....97
Go Scopes....99
Go Scopes vs. Go Source Files....102
Initializing Go Variables....104
Lifetimes of Go Identifiers....106
Go Module Summary....107
Go Assignments and Expressions....113
Text Formatting in Go....115
Goroutines....120
Issues with Concurrency....120
Go Concurrency....123
Goroutines by Example....126
Chapter 5: Go Basic Features....136
Language Keywords....136
Operators and Punctuation....139
Go Operator Precedence....144
Go Built-in Functions....145
Chapter 6: Go Types....147
Primitive/Built-in Types....147
Numbers....147
Characters and Strings....149
Reference vs. Pointer Types....150
Drill Down on Basic Data Types....153
Boolean Type....153
Integer Types....153
Floating-Point Types....154
When nil Can Go Wrong....155
Programmer-Defined Types....156
Arrays....161
Slices....163
Maps....168
Functions....171
Methods As Functions....176
Any Declared Type Can Have Custom Functions....177
Functions As Values....179
Structs....183
Structure Fields....186
Structure Literals....188
Nested Structures....189
Structure Field Alignment....191
Interfaces....192
Application of Interfaces....195
Composite Interfaces....198
Channels....199
Variable Declarations....203
Constant Declarations....206
Type Casting....208
Type Testing....209
Derived Type Declarations....212
Chapter 7: Errors and Panics....216
Go Errors....216
Go Panics....218
Errors and Panics Illustrated....222
Chapter 8: Go Statements....229
Package and Import Statements....230
Assignment Statements....231
Declaring Variables....233
Declaring Named Constants....235
If/Else Statements....236
Switch Statement....239
While Statement....242
Do-While Statement....243
For with Index Statement....243
For over a Collection Statement....244
Forever Statement....246
Break and Continue Statements....247
Goto Statement....248
Return Statement....249
Defer Statement....250
Go Statement....251
Select Statement....252
Chapter 9: Applications for Interfaces....254
An Interface Is the Key....254
On Dependency Injection....257
On Aspect-Oriented Programming....265
Chapter 10: Go Unit Tests and Benchmarks....270
Test Cases and Benchmarks in Go....271
Test Cases in Java....283
Chapter 11: Going into the Future....288
Improved Error Handling....288
Go Generics....290
Capstone Game of Life Example....296
Game of Life Go Source....301
Capstone Project in Java....302
Main.java....303
Server.java....310
Game.java....322
Utility.java....335
Formatter.java....336
ParameterProvider.java....340
AnnimatedGifWriter.java....341
Logger.java....344
Capstone Project in Go....347
Main.go....347
Server.go....352
Game.go....361
Utility.go....373
Go Doc Output....374
API Outputs....376
Game Outputs....380
Part III: Go Library Survey....383
Chapter 12: Key Package Comparison....395
Java Lang Packages....395
Java IO Package....400
Java Text Package....403
Java Time Packages....405
Java Util Packages....406
Chapter 13: Key Method/Function Comparison....413
Chapter 14: Go Package Survey....419
File Access with Go....419
Compression Services....422
Archive Packages....422
Compression Packages....425
Image....426
Input/Output (I/O)....429
Bytes Package....435
Format Package....439
Data Collections....441
Sorting....444
Context Package....446
Cryptography and Hashing and Data Encoding....450
Encoding Packages....451
Unicode Encoding Packages....453
Chapter 15: SQL Database Access....456
Chapter 16: Client and Server Support....467
MIME Packages....467
Network Packages....467
The Net Package....468
The HTTP Template Package....480
The Net.HTTP Package....485
URL Package....495
Chapter 17: Go Runtime....497
Errors Package....497
Flag Package....498
Log Package....503
Math Package....510
Operating System Support Packages....525
Reflection Package....533
Regular Expression Package....535
Go Runtime Packages....537
String Processing Packages....538
Concurrency and Goroutines....543
Testing Package....546
Time and Date Package....551
Appendix A: Installing Go....558
Appendix B: Some Go FAQs....569
Appendix C: Go Gotchas to Look Out For....577
Appendix D: Mark-Sweep Pseudocode....582
Appendix E: ASCII vs. UTF-8....586
Index....589
Get an in-depth introduction to the Go programming language and its associated standard runtime libraries. This book is targeted towards programmers that already know the Java programming language and uses that Java knowledge to direct the learning of Go. You will get a deep understanding of the Go language and obtain a good introduction to the extensive Go standard libraries.
This book teaches Go through clear descriptions of Go features, contrasting them with similar Java features and via providing extensive code examples. After reading this book you will be knowledgeable enough about Go and its libraries to begin doing effective programming using the Go language.
Go for Java Programmers is structured more like a tutorial than a reference document. It covers key features of Go, but not every little detail as a reference might. Its goal is to get you competent enough in Go and its runtime that you can begin to effectively writeGo programs.
Primarily existing professional Java programmers or students that already know something about Java. A basic understanding of Java is expected. Some basic programming experience with imperative languages is expected.