-
Go - Print struct with pretty look
摘要:package main import ( "fmt" ) type Person struct { Name string Age int Email string } func main() { p := Person{ Name: "Drake", Age: 35, Email: "drake
阅读全文
-
Go - Changing the Timing for Running Performance Tests
摘要:Problem: You want to run performance tests for a specific duration or a specific number of iterations. Solution: You can increase the minimum duration
阅读全文
-
Go - Avoiding Test Fixtures in Performance Tests
摘要:Problem: You want to customize the performance tests to avoid benchmarking test fixtures. Solution: You can start, stop, and reset the benchmark timer
阅读全文
-
Go - Testing a Web Application or a Web Service
摘要:Problem: You want to do unit testing on a web application or a web service. Solution: Use the httptest.NewRecorder function to create an httptest.Resp
阅读全文
-
Go - Generating Random Test Inputs for Tests
摘要:Problem: You want to generate random test data for running your test functions. Solution: Use fuzzing , which is an automated testing technique to gen
阅读全文
-
Go - Running Tests in Parallel
摘要:Problem: You want to speed up testing by running tests in parallel. Solution: Use the t.Parallel function to enable tests or subtests to run in parall
阅读全文
-
Go - Creating Subtests to Have Finer Control Over Groups of Test Cases
摘要:Problem: You want to create subtests within a test function to have finer control over test cases. Solution: Use the t.Run function to create subtests
阅读全文
-
Go - Setting Up and Tearing Down Before and After Tests
摘要:Problem: You want to set up data and an environment for testing and tear it down after the test is run. Solution: You can create helper functions or u
阅读全文
-
Go - Making an HTTP Client Request
摘要:Problem: You want to make an HTTP request to a web server. Solution: Use the net/http package to make an HTTP request. HTTP is a request-respond proto
阅读全文
-
Go - Using Templates for Go Web Applications
摘要:Problem: You want to use Go’s templating system to create a web application. Solution: Use the html/template package to create a web application. pack
阅读全文
-
Go - Serving Through HTTPS
摘要:Problem: You want to serve your web application through HTTPS. Solution: Use the http.ListenAndServeTLS function to serve your web application through
阅读全文
-
Go - Creating a JSON Web Service API
摘要:Problem: You want to create a simple web service API that returns JSON. Solution: Use the net/http package to create a web service API and the encodin
阅读全文
-
Go - Serving Static Files
摘要:Problem: You want to serve static files such as images, CSS, and JavaScript files. Solution: Use the http.FileServer function to serve static files. f
阅读全文
-
Go - Uploading a File to a Web Application
摘要:Problem: You want to upload a file to a web application. Solution: Use the net/http package to create a web application and the io package to read the
阅读全文
-
Go - Handling HTML Forms
摘要:Problem: You want to process data submitted from HTML forms. Solution: Use the Form field of http.Request or the FormValue method to access the data s
阅读全文
-
Go - Handling HTTP Requests
摘要:Problem: You want to process HTTP requests and send back HTTP responses. Solution: Use http.Request to extract information on HTTP requests and http.R
阅读全文
-
Go - Creating a Simple Web Application
摘要:Problem: You want to create a simple web application that responds to an HTTP request and sends back an HTTP response. Solution: Use the net/http pack
阅读全文
-
Go - Creating a UDP Client
摘要:Problem: You want to create a UDP client to send data to a UDP server. Solution: Use the Dial function in the net package to connect to a UDP server.
阅读全文
-
Go - Creating a UDP Server
摘要:Problem: You want to create a UDP server to receive data from a UDP client. Solution: Use the ListenPacket function in the net package to listen for i
阅读全文
-
Go - Creating a TCP Client
摘要:Problem: You want to create a TCP client to send data to a TCP server. Solution: Use the Dial function in the net package to connect to a TCP server.
阅读全文
-
Go - Creating a TCP Server
摘要:Problem: You want to create a TCP server to receive data from a TCP client. Solution: Use the Listen function in the net package to listen for connect
阅读全文
-
Znote - Network
摘要:Computer networks use network protocols to communicate with each other. Network protocols are often abstracted into different layers. For example, Ope
阅读全文
-
Go - Resizing an Image
摘要:Problem: You want to resize an image, making it larger or smaller. Solution: Convert an image to a grid of pixels as the source and create a new image
阅读全文
-
Go - Converting an Image to Grayscale
摘要:Problem: You want to convert the image to grayscale. Solution: Convert an image to a grid of pixels. Take each pixel in the grid and convert it to a g
阅读全文
-
Go - Flipping an Image Upside Down
摘要:Problem: You want to flip an image upside down. Solution: Convert an image to a grid of pixels. Swap the positions of the top and bottom pairs of pixe
阅读全文
-
Go - Creating Images
摘要:Problem: You want to create an image from scratch. Solution: Create one of the Image implementation structs (e.g., NRGBA ) and populate it with the ap
阅读全文
-
Go - Saving an Image to a File
摘要:Problem: You have an image and want to save it to a file. Solution: Use the Encode method of the correct file format package (e.g., png.Encode for PNG
阅读全文
-
Go - Loading an Image from a File
摘要:Problem: You want to load an image from an image file. Solution: Use image.Decode to decode data from an image file into an implementation of image.Im
阅读全文
-
Go - Image Processing
摘要:The standard library for 2D image manipulation is the image package and the main interface is image.Image . To work with the different image formats,
阅读全文
-
Go - Finding the Shortest Path on a Graph
摘要:Problem: You want to find the shortest path between two nodes on a weighted graph. Solution: Use Dijkstra’s algorithm to find the shortest path betwee
阅读全文
-
Go - Creating Graphs
摘要:Problem: You want to create a weighted graph data structure. Solution: Create structs for nodes and edges and place them in a Graph struct. Create and
阅读全文
-
Go - Creating Heaps
摘要:Problem: You want to create a min heap data structure. Solution: Wrap a struct around a slice of elements to represent a heap. After each push or pop
阅读全文
-
Go - Creating Linked Lists
摘要:Problem: You want to create a linked list data structure. Solution: Create an element struct that has a pointer to the next element. Wrap another stru
阅读全文
-
Go - Creating Sets
摘要:Problem: You want to create a set data structure. Solution: Wrap a struct around a map. Create set functions on the struct. A set is an unordered data
阅读全文
-
Go - Creating Stacks
摘要:Problem: You want to create a stack data structure. Solution: Wrap a struct around a slice. Create stack functions on the struct. A stack is a last-in
阅读全文
-
Go - Creating Queues
摘要:Problem: You want to create a queue data structure. Solution: Wrap a struct around a slice. Create queue functions on the struct. A queue is a first-i
阅读全文
-
Go - Sorting Maps
摘要:Problem: You want to sort a map by its keys. Solution: Get the keys of the map in a slice and sort that slice. Then, using the sorted slice of keys, i
阅读全文
-
Go - Sorting Arrays or Slices
摘要:Problem: You want to sort elements in an array or slice. Solution: For int , float64 , and string arrays or slices you can use sort.Ints , sort.Float6
阅读全文
-
Go - Making Arrays and Slices Safe for Concurrent Use
摘要:Problem: You want to make arrays and slices safe for concurrent use by multiple goroutines. Solution: Use a mutex from the sync library to safeguard t
阅读全文
-
Go - Remove values from a slice
摘要:To take out the first element of the slice: numbers := []int{3, 14, 159, 26, 53, 58} numbers = numbers[1:] // remove element 0 To take out the last el
阅读全文
-
Go - Insert values into a slice
摘要:There is no built-in function for insertion, but you can still use append for the task. Let’s say you want to insert the number 1000 between elements
阅读全文
-
Go - Defining Metadata for Struct Fields
摘要:Problem: You want to define metadata to describe the struct fields. Solution: Use struct tags to define metadata and the reflect package to access the
阅读全文
-
Go - Creating and Passing Struct Instances
摘要:Problem: You want to create an instance of a struct. Solution: Create a struct instance directly using the name of the struct, or a pointer to a struc
阅读全文
-
Go - Parsing Time Displays Into Structs
摘要:func main() { str := "4:31am +0800 on Oct 1, 2021" layout := "3:04pm -0700 on Jan 2, 2006" t, err := time.Parse(layout, str) if err != nil { log.Print
阅读全文
-
Go - Formatting time
摘要:func main() { t := time.Now() fmt.Println(t.Format("3:04PM")) fmt.Println(t.Format("Jan 02, 2006")) } When you run this you should see something like:
阅读全文
-
Go - Measuring Lapsed Time
摘要:Problem: You want to measure the lapsed time and make sure that it is accurate. Solution: Use the monotonic clock in the Time struct to find the lapse
阅读全文
-
Go - Representing Duration
摘要:Problem: You want to specify a duration of time. Solution: Use the Duration type to represent a span of time. The main representation for a span of ti
阅读全文
-
Go - Representing Time Zones
摘要:Problem: You want to include the time zone information in a Time struct. Solution: The Time struct includes a Location , which is the representation o
阅读全文
-
Go - Decoding Data with a Customized Binary Format to Structs
摘要:Problem: You want to decode the customized binary format back to structs. Solution: Use the encoding/binary package to take data from the binary forma
阅读全文
-
Go - Encoding Data to a Customized Binary Format
摘要:Problem: You want to encode struct data to a customized binary format. Solution: Design your customized format and use the encoding/binary package to
阅读全文
-
Go - Decoding gob Format Data to Structs
摘要:Problem: You want to decode gob format data back to structs. Solution: Use the encoding/gob package to decode the gob format data back to structs. fun
阅读全文
-
Go - Encoding Data to gob Format Data
摘要:Problem: You want to encode structs into binary gob format. Solution: Use the encoding/gob package to encode the structs into bytes that can be stored
阅读全文
-
Go - Creating JSON Data Streams from Structs
摘要:Problem: You want to create streaming JSON data from structs. Solution: Create an encoder using NewEncoder in the encoding/json package, passing it an
阅读全文
-
Go - Creating JSON Data from Structs
摘要:Problem: You want to create JSON data from a struct. Solution: Create the structs then use the json.Marshal or json.MarshalIndent to marshal the data
阅读全文
-
Go - Parsing JSON Data Streams Into Structs
摘要:Problem: You want to parse JSON data from a stream. Solution: Create structs to contain the JSON data. Create a decoder using NewDecoder in the encodi
阅读全文
-
Go - Performance Optimization
摘要:Using io.Copy is about twice as fast and uses only a fraction (less than 1%) of the memory. With really large files, if you’re using the io.ReadAll an
阅读全文
|