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

Mastering production applications concurrency cloud native 8 pdf

681 124 0

Đ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 681
Dung lượng 8 MB

Nội dung

Mastering Go Create Golang production applications using network libraries, concurrency, and advanced Go data structures Mihalis Tsoukalos BIRMINGHAM - MUMBAI Mastering Go Copyright © 2018 Packt Publishing All rights reserved No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews Every effort has been made in the preparation of this book to ensure the accuracy of the information presented However, the information contained in this book is sold without warranty, either express or implied Neither the author, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals However, Packt Publishing cannot guarantee the accuracy of this information Acquisition Editors: Frank Pohlmann, Suresh Jain Project Editor: Kishor Rit Content Development Editor: Gary Schwarts Technical Editors: Gaurav Gavas, Nidhisha Shetty Proofreader: Tom Jacob Indexer: Mariammal Chettiyar Graphics: Tom Scaria Production Coordinator: Shantanu Zagade First published: April 2018 Production reference: 1270418 Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 978-1-78862-654-5 www.packtpub.com Packt Upsell mapt.io Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career For more information, please visit our website Why subscribe? Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals Improve your learning with Skill Plans built especially for you Get a free eBook or video every month Mapt is fully searchable Copy and paste, print, and bookmark content PacktPub.com Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy Get in touch with us at service@packtpub.com for more details At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks Contributors About the author Mihalis Tsoukalos is a technical author, a Unix administrator, a developer, and a mathematician, who enjoys learning new things He has written more than 250 technical articles for many publications, including Sys Admin, MacTech, Linux User and Developer, Usenix ;login:, Linux Format, and Linux Journal Mihalis is also the author of Go Systems Programming, by Packt Publishing, 2017 and the technical editor for MongoDB in Action, Second Edition, by Manning Mihalis' research interests include databases, operating systems, and statistics You can reach him at ht tp://www.mtsoukalos.eu/ and @mactsouk He is also a photographer ( http://www.h ighiso.net/) I would like to thank the people at Packt Publishing for helping me write this book, including Frank Pohlmann and Gary Schwartz, my technical reviewer, Mat Ryer, Radhika Atitkar, for her encouragement and trust, and Kishor Rit, for answering all my questions and encouraging me during the whole process For all people everywhere: You will never change your life until you change something you daily! Doing low-level network programming Although the http.Transport structure allows you to modify the various low-level parameters of a network connection, you can write Go code that permits you to read the raw data of network packets There are two tricky points here: Network packets come in binary format, which requires you to look for specific kinds of network packets and not just any type of network packet Put simply, when reading network packets, you should specify the protocol or protocols that you will support in your applications in advance In order to send a network packet, you will have to construct it on your own The next utility to be shown is called lowLevel.go, and it will be presented in three parts Note that lowLevel.go captures ICMP packets, which use the IPv4 protocol and prints their contents Also note that working with raw network data requires root privileges for security reasons The first segment of lowLevel.go is as follows: package main import ( "fmt" "net" ) The second part of lowLevel.go contains the following Go code: func main() { netaddr, err := net.ResolveIPAddr("ip4", "127.0.0.1") if err != nil { fmt.Println(err) return } conn, err := net.ListenIP("ip4:icmp", netaddr) if err != nil { fmt.Println(err) return } The ICMP protocol is specified in the second part of the first parameter (ip4:icmp) of net.ListenIP() Moreover, the ip4 part tells the utility to capture IPv4 traffic only The remaining part of lowLevel.go contains the following Go code: buffer := make([]byte, 1024) n, _, err := conn.ReadFrom(buffer) if err != nil { fmt.Println(err) return } fmt.Printf("% X\n", buffer[0:n]) } The preceding code tells lowLevel.go to read just a single network packet because there is no for loop The ICMP protocol is used by the ping(1) and traceroute(1) utilities, so one way to create ICMP traffic is to use one of these two tools The network traffic will be generated using the following commands on both Unix machines while lowLevel.go is already running: $ ping -c localhost PING localhost (127.0.0.1): 56 data 64 bytes from 127.0.0.1: icmp_seq=0 64 bytes from 127.0.0.1: icmp_seq=1 64 bytes from 127.0.0.1: icmp_seq=2 64 bytes from 127.0.0.1: icmp_seq=3 64 bytes from 127.0.0.1: icmp_seq=4 bytes ttl=64 ttl=64 ttl=64 ttl=64 ttl=64 time=0.037 time=0.038 time=0.117 time=0.052 time=0.049 ms ms ms ms ms - localhost ping statistics packets transmitted, packets received, 0.0% packet loss round-trip min/avg/max/stddev = 0.037/0.059/0.117/0.030 ms $ traceroute localhost traceroute to localhost (127.0.0.1), 64 hops max, 52 byte packets localhost (127.0.0.1) 0.255 ms 0.048 ms 0.067 ms Executing lowLevel.go on a macOS High Sierra machine with root privileges will produce the following output: $ sudo go run lowLevel.go 03 03 CD DA 00 00 00 00 45 00 34 00 B4 0F 00 00 01 11 00 00 7F 00 00 01 7F 00 00 01 B4 0E 82 9B 00 20 00 00 $ sudo go run lowLevel.go 00 00 0B 3B 20 34 00 00 5A CB 5C 15 00 04 32 A9 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 The first output example is generated by the ping(1) command, whereas the second output example is generated by the traceroute(1) command Running lowLevel.go on a Debian Linux machine will generate the following output: $ uname -a Linux mail 4.14.12-x86_64-linode92 #1 SMP Fri Jan 15:34:44 UTC 2018 x86_64 GNU/Linux # go run lowLevel.go 08 00 61 DD 3F BA 00 01 9A 5D CB 5A 00 00 00 00 26 DC 0B 00 00 00 00 00 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F 20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F 30 31 32 # go run 03 03 BB CB 40 82 54 55 56 33 34 35 36 lowLevel.go B8 00 00 00 9A 00 28 FE 57 58 59 5A 37 00 45 00 00 3C CD 8D 00 00 01 11 EE 21 7F 00 00 01 7F 00 00 01 3B 40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 5B 5C 5D 5E 5F The output of the uname(1) command prints useful information about the Linux system Note that on modern Linux machines you should execute ping(1) with the -4 flag in order to tell it to use the IPv4 protocol Grabbing raw ICMP network data In this subsection, you will learn how to use the syscall package to capture raw ICMP network data and syscall.SetsockoptInt() in order to set the options of a socket Keep in mind that sending raw ICMP data is much more difficult, as you will have to construct the raw network packets on your own The name of the utility is syscallNet.go, and it will be shown in four parts The first part of syscallNet.go is as follows: package main import ( "fmt" "os" "syscall" ) The second segment of syscallNet.go contains the following Go code: func main() { fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_ICMP) if err != nil { fmt.Println("Error in syscall.Socket:", err) return } f := os.NewFile(uintptr(fd), "captureICMP") if f == nil { fmt.Println("Error in os.NewFile:", err) return } The syscall.AF_INET parameter tells syscall.Socket() that you want to work with IPv4 The syscall.SOCK_RAW parameter is what makes the generated socket a raw socket The last parameter, which is syscall.IPPROTO_ICMP, tells syscall.Socket() that you are interested in ICMP traffic only The third part of syscallNet.go is as follows: err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_RCVBUF, 256) if err != nil { fmt.Println("Error in syscall.Socket:", err) return } The call to syscall.SetsockoptInt() sets the size of the receive buffer of the socket to 256 The syscall.SOL_SOCKET parameter is for stating that you want to work on the socket layer level The remaining Go code of syscallNet.go is as follows: for { buf := make([]byte, 1024) numRead, err := f.Read(buf) if err != nil { fmt.Println(err) } fmt.Printf("% X\n", buf[:numRead]) } } Due to the for loop, syscallNet.go will keep capturing ICMP network packets until you terminate it manually Executing syscallNet.go on a macOS High Sierra machine will produce the following type of output: $ sudo go run syscallNet.go 45 00 40 00 BC B6 00 00 40 01 5A CB 6A 90 00 0B 9F 1A 08 09 1C 1D 1E 1F 20 21 22 23 24 25 45 00 40 00 62 FB 00 00 40 01 5A CB 6A 91 00 0B AC 5F 08 09 1C 1D 1E 1F 20 21 22 23 24 25 45 00 40 00 9A 5F 00 00 40 01 5A CB 6A 92 00 0B C0 76 08 09 1C 1D 1E 1F 20 21 22 23 24 25 45 00 40 00 6E 0D 00 00 40 01 5A CB 6A 93 00 0B D4 7B 08 09 1C 1D 1E 1F 20 21 22 23 24 25 45 00 40 00 3A 07 00 00 40 01 5A CB 6A 94 00 0B DF AB 08 09 1C 1D 1E 1F 20 21 22 23 24 25 45 00 24 00 45 55 00 00 40 01 45 00 34 00 C5 73 00 00 01 11 45 00 24 00 E8 1E 00 00 40 01 45 00 34 00 C5 74 00 00 01 11 45 00 24 00 2A 4B 00 00 40 01 45 00 34 00 C5 75 00 00 01 11 00 0A 26 00 0A 26 00 0A 26 00 0A 26 00 0A 26 00 00 00 00 00 00 00 0B 27 00 0B 27 00 0B 27 00 0B 27 00 0B 27 00 00 00 00 00 00 7F 0C 28 7F 0C 28 7F 0C 28 7F 0C 28 7F 0C 28 7F 7F 7F 7F 7F 7F 00 0D 29 00 0D 29 00 0D 29 00 0D 29 00 0D 29 00 00 00 00 00 00 00 0E 2A 00 0E 2A 00 0E 2A 00 0E 2A 00 0E 2A 00 00 00 00 00 00 01 0F 2B 01 0F 2B 01 0F 2B 01 0F 2B 01 0F 2B 01 01 01 01 01 01 7F 10 2C 7F 10 2C 7F 10 2C 7F 10 2C 7F 10 2C 7F 7F 7F 7F 7F 7F 00 11 2D 00 11 2D 00 11 2D 00 11 2D 00 11 2D 00 00 00 00 00 00 00 12 2E 00 12 2E 00 12 2E 00 12 2E 00 12 2E 00 00 00 00 00 00 01 13 2F 01 13 2F 01 13 2F 01 13 2F 01 13 2F 01 01 01 01 01 01 00 14 30 00 14 30 00 14 30 00 14 30 00 14 30 03 C5 03 C5 03 C5 00 15 31 00 15 31 00 15 31 00 15 31 00 15 31 03 72 03 72 03 72 3F 16 32 31 16 32 1D 16 32 09 16 32 FE 16 32 AB 82 AB 82 AB 82 36 17 33 EF 17 33 D6 17 33 CF 17 33 9C 17 33 12 9B 10 9C 0E 9D 71 18 34 71 18 34 71 18 34 71 18 34 71 18 34 00 00 00 00 00 00 45 19 35 45 19 35 45 19 35 45 19 35 45 19 35 00 20 00 20 00 20 00 1A 36 00 1A 36 00 1A 36 00 1A 36 00 1A 36 00 00 00 00 00 00 00 1B 37 01 1B 37 02 1B 37 03 1B 37 04 1B 37 00 00 00 00 00 00 Running syscallNet.go on a Debian Linux machine will generate the following output: # go run 45 00 00 FA 6A CB 1C 1D 1E 45 00 00 FA 6A CB 1C 1D 1E 45 C0 00 45 00 00 52 F1 AB 45 00 00 FB 6A CB 1C 1D 1E 45 00 00 FB 6A CB 1C 1D 1E 45 00 00 FC 6A CB 1C 1D 1E 45 00 00 FC 6A CB syscallNet.go 54 7F E9 40 00 5A 00 00 00 00 1F 20 21 22 23 54 7F EA 00 00 5A 00 00 00 00 1F 20 21 22 23 44 68 54 00 00 28 40 4F 40 00 DA 50 14 00 00 54 80 4E 40 00 5A 00 00 00 00 1F 20 21 22 23 54 80 4F 00 00 5A 00 00 00 00 1F 20 21 22 23 54 80 9B 40 00 5A 00 00 00 00 1F 20 21 22 23 54 80 9C 00 00 5A 00 00 00 00 40 AA 24 40 AA 24 34 34 90 40 9A 24 40 9A 24 40 83 24 40 83 01 7B 25 01 7B 25 01 06 9E 01 80 25 01 80 25 01 94 25 01 94 BC 06 26 FC 06 26 8B 74 00 BC 06 26 FC 06 26 BC 06 26 FC 06 BD 00 27 BC 00 27 8E 6A 00 58 00 27 57 00 27 0B 00 27 0A 00 7F 00 28 7F 00 28 86 6D 00 00 29 00 00 29 77 4A 00 00 2A 00 00 2A DC C1 01 00 2B 01 00 2B 57 FD 7F 10 2C 7F 10 2C 6D 86 00 11 2D 00 11 2D 4A 77 00 12 2E 00 12 2E C1 DC 01 13 2F 01 13 2F FD 57 08 14 30 00 14 30 03 B0 00 15 31 00 15 31 0A B8 6F 16 32 77 16 32 8F DD 07 17 33 07 17 33 27 96 53 18 34 53 18 34 00 00 E3 19 35 E3 19 35 00 00 00 1A 36 00 1A 36 00 00 01 1B 37 01 1B 37 00 00 7F 00 28 7F 00 28 7F 00 28 7F 00 00 00 29 00 00 29 00 00 29 00 00 00 00 2A 00 00 2A 00 00 2A 00 00 01 00 2B 01 00 2B 01 00 2B 01 00 7F 10 2C 7F 10 2C 7F 10 2C 7F 10 00 11 2D 00 11 2D 00 11 2D 00 11 00 12 2E 00 12 2E 00 12 2E 00 12 01 13 2F 01 13 2F 01 13 2F 01 13 08 14 30 00 14 30 08 14 30 00 14 00 15 31 00 15 31 00 15 31 00 15 7E 16 32 86 16 32 93 16 32 9B 16 01 17 33 01 17 33 EC 17 33 EC 17 53 18 34 53 18 34 53 18 34 53 18 E3 19 35 E3 19 35 E3 19 35 E3 19 00 1A 36 00 1A 36 00 1A 36 00 1A 02 1B 37 02 1B 37 03 1B 37 03 1B 1C 45 45 6C 45 FD 1C 45 FD 1C 45 FE 1C 45 FE 1C 45 45 C3 45 45 CF 45 45 40 5C 45 45 40 5C 45 45 40 5C 45 45 40 5C 45 45 40 5C 45 45 40 5C 45 45 40 5C 45 45 40 5C 45 45 40 5C 45 45 40 5C 45 45 40 5C 45 45 40 5C 45 45 40 1D C0 00 6E 00 6A 1D 00 6A 1D 00 6A 1D 00 6A 1D C0 00 D6 C0 00 DD C0 00 41 5D C0 00 41 5D C0 00 41 5D C0 00 41 5D C0 00 41 5D C0 00 41 5D C0 00 41 5D C0 00 41 5D C0 00 41 5D C0 00 41 5D C0 00 41 5D C0 00 41 5D C0 00 41 1E 00 00 D3 00 CB 1E 00 CB 1E 00 CB 1E 00 CB 1E 00 00 44 00 00 DB 00 00 42 5E 00 00 42 5E 00 00 42 5E 00 00 42 5E 00 00 42 5E 00 00 42 5E 00 00 42 5E 00 00 42 5E 00 00 42 5E 00 00 42 5E 00 00 42 5E 00 00 42 5E 00 00 42 1F 44 28 36 54 5A 1F 54 5A 1F 54 5A 1F 54 5A 1F 44 28 57 44 28 BE 58 3C 43 5F 58 3C 43 5F 58 3C 43 5F 58 3C 43 5F 58 3C 43 5F 58 3C 43 5F 58 3C 43 5F 58 3C 43 5F 58 3C 43 5F 58 3C 43 5F 58 3C 43 5F 58 3C 43 5F 58 3C 43 20 68 40 50 80 00 20 80 00 20 82 00 20 82 00 20 68 41 50 68 44 50 94 85 44 21 55 D1 14 F8 00 21 F9 00 21 0D 00 21 0E 00 21 56 74 14 57 27 14 B4 E1 45 22 00 40 00 40 00 22 00 00 22 40 00 22 00 00 22 00 40 00 00 40 00 00 00 46 23 00 00 00 00 00 23 00 00 23 00 00 23 00 00 23 00 00 00 00 00 00 00 00 47 24 34 34 71 40 23 24 40 23 24 40 CA 24 40 CA 24 34 34 09 34 33 CE 40 01 48 25 01 06 EF 01 98 25 01 98 25 01 FC 25 01 FC 25 01 06 5A 01 06 C3 01 11 49 26 8B 73 00 BB 06 26 FB 06 26 BA 06 26 FA 06 26 8B 73 00 8B 71 00 E7 35 4A 27 8D E8 00 AE 00 27 AD 00 27 99 00 27 98 00 27 8C 45 00 8B 92 00 2E CE 4B 28 29 2A 2B 2C 2D 2E 2F 30 31 32 33 34 35 36 37 86 77 DC 57 6D 4A C1 FD 03 0A 8F 27 00 00 00 00 6D 4A C1 FD 86 77 DC 57 8E 8E DD 96 00 00 00 00 7F 00 28 7F 00 28 7F 00 28 7F 00 28 86 6D 00 00 29 00 00 29 00 00 29 00 00 29 77 4A 00 00 2A 00 00 2A 00 00 2A 00 00 2A DC C1 01 00 2B 01 00 2B 01 00 2B 01 00 2B 57 FD 7F 10 2C 7F 10 2C 7F 10 2C 7F 10 2C 6D 86 00 11 2D 00 11 2D 00 11 2D 00 11 2D 4A 77 00 12 2E 00 12 2E 00 12 2E 00 12 2E C1 DC 01 13 2F 01 13 2F 01 13 2F 01 13 2F FD 57 08 14 30 00 14 30 08 14 30 00 14 30 03 2E 00 15 31 00 15 31 00 15 31 00 15 31 0A 9B F2 16 32 FA 16 32 4A 16 32 52 16 32 8F DD E7 17 33 E7 17 33 82 17 33 82 17 33 27 96 53 18 34 53 18 34 53 18 34 53 18 34 00 00 E3 19 35 E3 19 35 E3 19 35 E3 19 35 00 00 00 1A 36 00 1A 36 00 1A 36 00 1A 36 00 00 04 1B 37 04 1B 37 05 1B 37 05 1B 37 00 00 86 77 DC 57 6D 4A C1 FD 03 0A 8F 27 00 00 00 00 6D 4A C1 FD 86 77 DC 57 C5 C2 DD 96 00 00 00 00 7F 00 00 01 7F 00 00 01 03 03 F1 DA 00 00 00 00 7F 00 00 01 7F 00 00 01 95 1E 82 9A 00 28 FE 3B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 B5 00 00 40 01 E7 2D 7F 00 00 01 7F 00 00 01 03 03 F9 EA 00 00 00 00 85 E2 00 00 01 11 35 CD 7F 00 00 01 7F 00 00 01 8D 0D 82 9B 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 B6 00 00 40 01 E7 2C 7F 00 00 01 7F 00 00 01 03 03 D2 EB 00 00 00 00 85 E3 00 00 01 11 35 CC 7F 00 00 01 7F 00 00 01 B4 0B 82 9C 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 B7 00 00 40 01 E7 2B 7F 00 00 01 7F 00 00 01 03 03 D6 AC 00 00 00 00 85 E4 00 00 02 11 34 CB 7F 00 00 01 7F 00 00 01 B0 49 82 9D 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 B8 00 00 40 01 E7 2A 7F 00 00 01 7F 00 00 01 03 03 F1 B4 00 00 00 00 85 E5 00 00 02 11 34 CA 7F 00 00 01 7F 00 00 01 95 40 82 9E 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 B9 00 00 40 01 E7 29 7F 00 00 01 7F 00 00 01 03 03 CD 43 00 00 00 00 85 E6 00 00 02 11 34 C9 7F 00 00 01 7F 00 00 01 B9 B0 82 9F 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 BA 00 00 40 01 E7 28 7F 00 00 01 7F 00 00 01 03 03 9D 8F 00 00 00 00 85 E7 00 00 03 11 33 C8 7F 00 00 01 7F 00 00 01 E9 63 82 A0 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 BB 00 00 40 01 E7 27 7F 00 00 01 7F 00 00 01 03 03 A3 13 00 00 00 00 85 E8 00 00 03 11 33 C7 7F 00 00 01 7F 00 00 01 E3 DE 82 A1 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 BC 00 00 40 01 E7 26 7F 00 00 01 7F 00 00 01 03 03 D4 66 00 00 00 00 85 E9 00 00 03 11 33 C6 7F 00 00 01 7F 00 00 01 B2 8A 82 A2 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 BD 00 00 40 01 E7 25 7F 00 00 01 7F 00 00 01 03 03 A6 8D 00 00 00 00 85 EA 00 00 04 11 32 C5 7F 00 00 01 7F 00 00 01 E0 62 82 A3 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 BE 00 00 40 01 E7 24 7F 00 00 01 7F 00 00 01 03 03 F1 C6 00 00 00 00 85 EB 00 00 04 11 32 C4 7F 00 00 01 7F 00 00 01 95 28 82 A4 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 BF 00 00 40 01 E7 23 7F 00 00 01 7F 00 00 01 03 03 A3 FE 00 00 00 00 85 EC 00 00 04 11 32 C3 7F 00 00 01 7F 00 00 01 E2 EF 82 A5 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 C0 00 00 40 01 E7 22 7F 00 00 01 7F 00 00 01 03 03 B9 AA 00 00 00 00 85 ED 00 00 05 11 31 C2 7F 00 00 01 7F 00 00 01 CD 42 82 A6 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 5C 45 45 40 5C 45 45 40 5C 45 45 40 5C 5D C0 00 41 5D C0 00 41 5D C0 00 41 5D 5E 00 00 42 5E 00 00 42 5E 00 00 42 5E 5F 58 3C 43 5F 58 3C 43 5F 58 3C 43 5F 94 C1 00 00 40 01 E7 21 7F 00 00 01 7F 00 00 01 03 03 B3 B7 00 00 00 00 85 EE 00 00 05 11 31 C1 7F 00 00 01 7F 00 00 01 D3 34 82 A7 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 C2 00 00 40 01 E7 20 7F 00 00 01 7F 00 00 01 03 03 F2 62 00 00 00 00 85 EF 00 00 05 11 31 C0 7F 00 00 01 7F 00 00 01 94 88 82 A8 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B 94 C3 00 00 40 01 E7 1F 7F 00 00 01 7F 00 00 01 03 03 DD BE 00 00 00 00 85 F0 00 00 06 11 30 BF 7F 00 00 01 7F 00 00 01 A9 2B 82 A9 00 28 FE 3B 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F 50 51 52 53 54 55 56 57 58 59 5A 5B Where to go next? Philosophically speaking, no programming book can ever be perfect and neither is this one! Did I leave some Go topics out? Absolutely yes! Why? Because there are always more topics to cover in a book, so if I tried to cover everything, the book would never be ready for publication! This situation is somehow analogous to the specifications of a program—you can always add new and exciting features, but if you not freeze its specifications, the program will always be in development and it will never be ready for your intended audience! Some of the Go topics I missed might even appear in the second edition of this book! The good thing is that after reading this book, you will be ready to learn on your own, which is the biggest benefit that you can get from any good computer programming book The main purpose of this book is to help you learn how to program in Go and gain some experience in doing so However, there is no substitute for trying things out on your own, and failing often becomes the only way to learn a programming language, so you must keep developing nontrivial things So, this is the end of yet another Go book for me, but the beginning of a journey for you! Thanks for buying this book You are now ready to start writing your own software in Go and learning new things! Additional resources Take a look at the following resources: Visit the documentation of the net standard Go package, which can be found at https://golang.org/pkg/net/ This is one of the biggest documentation found in the Go documentation Although this book talked about RPC, it did not talk about gRPC, which is an open source, high-performance RPC framework A package with the Go language implementation of gRPC can be found at https://github.com/grpc/grpc-go The ICMP protocol for IPv4 is defined in RFC 792 It can be found in many places, including https://tools.ietf.org/html/rfc792 WebSocket is a protocol for two-way communication between a client and a remote host There is a WebSocket implementation for Go at https://github.com/gorilla/websocket You can learn more about WebSocket at http://www.rfceditor.org/rfc/rfc6455.txt If you are really into network programming and you want to be able to work with RAW TCP packets, you mind find interesting and helpful information and tools in the gopacket library, which can be found at https://github.com/google/gopacket The raw package, which is located at https://github.com/mdlayher/raw allows you to read and write data at the device driver level for a network device Exercises Develop a FTP client in Go Next try to develop a FTP server in Go Is it more difficult to implement the FTP client or the FTP server? Why? Try to implement a Go version of the nc(1) utility The secret when programming such fairly complex utilities is to start with a version that implements the basic functionality of the original utility before trying to support every possible option Modify TCPserver.go so that it returns the date in one network packet and the time in another Modify TCPserver.go so that it can serve multiple clients in a sequential way Note that this is not the same as being able to serve multiple requests concurrently Put simply, use a for loop so that the Accept() call can be executed multiple times TCP servers, such as fiboTCP.go, tend to terminate when they receive a given signal, so add signal handling code to fiboTCP.go, as you learned in Chapter 8, Telling a Unix System What to Do Modify kvTCP.go so that the save() function is protected using sync.Mutex Is this required? Develop your own small web server in Go using a plain TCP instead of using the http.ListenAndServe() function Summary This chapter addressed many interesting things including developing UDP and TCP clients and servers, which are applications that work over TCP/IP computer networks As this is the last chapter of this book, I would like to congratulate and thank you for choosing this book I hope that you found it useful and that you will continue using it as a reference throughout your Go journey Soli Deo gloria Other Books You May Enjoy If you enjoyed this book, you may be interested in these other books by Packt: Go Systems Programming Mihalis Tsoukalos ISBN: 978-1-78712-564-3 Explore the Go language from the standpoint of a developer conversant with Unix, Linux, and so on Understand Goroutines, the lightweight threads used for systems and concurrent applications Learn how to translate Unix and Linux systems code in C to Golang code How to write fast and lightweight server code Dive into concurrency with Go Write low-level networking code Distributed Computing with Go V.N Nikhil Anurag ISBN: 978-1-78712-538-4 Gain proficiency with concurrency and parallelism in Go Learn how to test your application using Go's standard library Learn industry best practices with technologies such as REST, OpenAPI, Docker, and so on Design and build a distributed search engine Learn strategies on how to design a system for web scale Isomorphic Go Kamesh Balasubramanian ISBN: 978-1-78839-418-5 Create Go programs inside the web browser using GopherJS Render isomorphic templates on both the client side and the server side Perform end-to-end application routing for greater search engine discoverability and an enhanced user experience Implement isomorphic handoff to seamlessly transition state between the web server and the web browser Build real-time web application functionality with websockets to promote user collaboration Create reusable components (cogs) that are rendered using the virtual DOM Deploy an Isomorphic Go application for production use Security with Go John Daniel Leon ISBN: 978-1-78862-791-7 Learn the basic concepts and principles of secure programming Write secure Golang programs and applications Understand classic patterns of attack Write Golang scripts to defend against network-level attacks Learn how to use Golang security packages Apply and explore cryptographic methods and packages Learn the art of defending against brute force attacks Secure web and cloud applications Leave a review - let other readers know what you think Please share your thoughts on this book with others by leaving a review on the site that you bought it from If you purchased the book from Amazon, please leave us an honest review on this book's Amazon page This is vital so that other potential readers can see and use your unbiased opinion to make purchasing decisions, we can understand what our customers think about our products, and our authors can see your feedback on the title that they have worked with Packt to create It will only take a few minutes of your time, but is valuable to other potential customers, our authors, and Packt Thank you! .. .Mastering Go Create Golang production applications using network libraries, concurrency, and advanced Go data structures Mihalis Tsoukalos BIRMINGHAM - MUMBAI Mastering Go Copyright © 20 18. .. Indexer: Mariammal Chettiyar Graphics: Tom Scaria Production Coordinator: Shantanu Zagade First published: April 20 18 Production reference: 12704 18 Published by Packt Publishing Ltd Livery Place... Published by Packt Publishing Ltd Livery Place 35 Livery Street Birmingham B3 2PB, UK ISBN 9 78- 1- 788 62-654-5 www.packtpub.com Packt Upsell mapt.io Mapt is an online digital library that gives

Ngày đăng: 21/03/2019, 09:23

TỪ KHÓA LIÊN QUAN