gophersnippets is a collection of code snippets with tests and testable examples for the Go programming language.
Each snippet:
- is small and shows how to achieve something specific with Go
- contains at least one test or testable example
- runs on Go playground since it now supports tests and testable examples
- How to create multiple loggers
- How to disable log output
- How to include the filename and the line number in a logger output
- How to format Go code programmatically
- How to print the binary representation of an integer
- How to print the same variable multiple times using printf
- How to print Go version
- How to print information about the operating system, architecture and pointer size
- How to check if a slice contains a specific element
- How to delete an element from a slice
- How to filter a slice
- How to find the maximum element of a slice
- How to find the minimum element of a slice
- How to remove duplicate elements from a slice
- How to reverse a slice
- How to shuffle a slice
- How to split a slice in chunks
- How to calculate the hamming distance between two strings
- How to check if a string ends with a substring
- How to check if a string is lowercase
- How to check if a string is uppercase
- How to check if a string starts with substring
- How to check if string is valid JSON
- How to concatenate strings
- How to count the number of words in a string
- How to define a custom string representation for a type
- How to get the md5 checksum of a string
- How to get the sha1 checksum of a string
- How to get the sha256 checksum of a string
- How to pretty print JSON
- How to reverse a string
- How to check that a time value is a specific day of the week
- How to measure the execution time of a function
https://gophersnippets.com uses Github issues for comments, powered by https://utteranc.es/. You can add comments directly on the corresponding Github issue or use the comment form under each code section.
You are welcome to contribute testable Go code snippets.
Please read the following before submitting a PR:
- https://blog.golang.org/examples
- https://blog.golang.org/playground
- https://github.com/golang/go/wiki/TableDrivenTests
- Include only one code snippet per PR
- The snippet should be small and solve one problem only
- Include comments when necessary
- When you submit a PR the following checks will run automatically:
-
code includes at least 1 test or testable example
-
code runs successfully on play.golang.org
-
code passes go vet
-
code includes a category and a title in the first line of the code (Category: Title)
// Strings: How to reverse a string // package main
or includes a category and title in the first line of the code, an empty comment line and then a description
// Strings: How to reverse a string // // Description // package main
-
code should be included in one file only, named main_test.go. This is necessary in order to test and run snippets locally
-
folder should match the slug version of the title:
if the title is How to reverse a string,
the folder should be snippets/how-to-reverse-a-string
and the full path should be snippets/how-to-reverse-a-string/main_test.go
-
Do no edit anything inside docs folder. It is generated automatically on succesful merge
-
Do no edit README.md. It is generated automatically on succesful merge. Edit https://github.com/psampaz/gophersnippets/edit/contributing/tools/templates/readme.md instead.
-
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.