All the tables provided in the cheat sheets are also presented in tables below which are easy to copy and paste.
View or Download the Cheat Sheet JPG image
Right-click on the image below to save the JPG file (2500 width x 1981 height in pixels), or click here to open it in a new browser tab. Once the image opens in a new window, you may need to click on the image to zoom in and view the full-sized JPG.
The Go Network Programming Cheat Sheet covers:
Operators
Variables
Packages
Comments
Reserved Keywords
Controls
Arrays, Slices, & Ranges
Functions
TCP Socket Programming with Go
Creating a TCP client with Go
Creating a TCP Server with Go
Testing the TCP Client and Server with Go
AWS Lambda Function Handler example with Go
AWS Bucket List example with Go
Creating a TCP client with Go
Creating a TCP Server with Go
Testing the TCP Client and Server with Go
View or Download the cheat sheet PDF file
Download the cheat sheet PDF file here. When it opens in a new browser tab, simply right click on the PDF and navigate to the download menu.
What’s included in this cheat sheet
The following categories and items have been included in the cheat sheet:
Operators
Variables
Packages
Comments
Reserved Keywords
Controls
Arrays, Slices, & Ranges
Functions
TCP Socket Programming with Go
Creating a TCP client with Go
//create a file named tcpC.go
package main
import ( “bufio” “fmt” “net” “os” “strings” )
func main() { arguments := os.Args if len(arguments) == 1 { fmt.Println(“Please provide host:port.”) return }
CONNECT := arguments[1]
c, err := net.Dial("tcp", CONNECT)
if err != nil {
fmt.Println(err)
return
}
for {
reader := bufio.NewReader(os.Stdin)
fmt.Print(">> ")
text, _ := reader.ReadString('\n')
fmt.Fprintf(c, text+"\n")
message, _ := bufio.NewReader(c).ReadString('\n')
fmt.Print("->: " + message)
if strings.TrimSpace(string(text)) == "STOP" {
fmt.Println("TCP client exiting...")
return
}
}
}
Creating a TCP Server with Go
//create a file named tcpS.go
package main
import ( “bufio” “fmt” “net” “os” “strings” “time” )
func main() { arguments := os.Args if len(arguments) == 1 { fmt.Println(“Please provide port number”) return }
PORT := ":" + arguments[1]
l, err := net.Listen("tcp", PORT)
if err != nil {
fmt.Println(err)
return
}
defer l.Close()
c, err := l.Accept()
if err != nil {
fmt.Println(err)
return
}
for {
netData, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
fmt.Println(err)
return
}
if strings.TrimSpace(string(netData)) == "STOP" {
fmt.Println("Exiting TCP server!")
return
}
fmt.Print("-> ", string(netData))
t := time.Now()
myTime := t.Format(time.RFC3339) + "\n"
c.Write([]byte(myTime))
}
}
Testing the TCP Client and Server with Go
//Run your TCP server. From the directory containing the tcpS.go file, run the following command:
go run tcpS.go 1234
//The server will listen on port number 1234. You will not see any output as a result of this command. //Open a second shell session to execute the TCP client and to interact with the TCP server. Run the following command:
go run tcpC.go 127.0.0.1:1234
//Note: If the TCP server is not running on the expected TCP port, you will get the following error message from tcpC.go:
dial tcp [::1]:1234: connect: connection refused
//You will see a » prompt waiting for you to enter some text. Type in Hello! to receive a response from the TCP server:
Hello!
//You should see a similar output:
Hello! ->: 2019-05-23T19:43:21+03:00
//Send the STOP command to exit the TCP client and server:
STOP
//You should see a similar output in the client:
STOP ->: TCP client exiting…
//The output on the TCP server side will resemble the following:
-> Hello! Exiting TCP server!
AWS Lambda Function Handler example with Go
package main
import ( “fmt” “github.com/aws/aws-lambda-go/lambda” )
type MyEvent struct { Name string ‘json:“What is your name?”’ Age int ‘json:“How old are you?”’ }
type MyResponse struct { Message string ‘json:“Answer:”’ }
func HandleLambdaEvent(event MyEvent) (MyResponse, error) { return MyResponse{Message: fmt.Sprintf("%s is %d years old!", event.Name,\event.Age)}, nil }
func main() { lambda.Start(HandleLambdaEvent) }
AWS Bucket List example with Go
package main
import ( “fmt”
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
func main() { s3svc := s3.New(session.New()) result, err := s3svc.ListBuckets(&s3.ListBucketsInput{}) if err != nil { fmt.Println(“Buckets list failed”, err) return }
fmt.Println("Buckets:")
for _, bucket := range result.Buckets {
fmt.Printf("%s : %s\n", aws.StringValue(bucket.Name), bucket.CreationDate)
}
}
Reference and further reading
- TCP Socket programming – Code Attribution: https://www.linode.com/docs/development/go/developing-udp-and-tcp-clients-and-servers-in-go/#create-the-tcp-client
- AWS Lambda Function Handler in Go – Based on AWS docs: https://docs.aws.amazon.com/lambda/latest/dg/golang-handler.html
- GoLang Reference Website: golang.org