Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

203 Commits

Repository files navigation

Exploring Golang features Go Report Card

You can incorporate these code snippets into your larger programming modules. This repository is viewable on sourcegraph.com.

Go

Contents

NameDescription
00_persisting_goInstalling Go, persisting on systemd, Upstart
01_data_typetype, error, recursion, reference, sort, switch, type assertion
02_mongodb_aggregate_cliaggregate data from mongodb displayed in CLI
03_mongodb_find_sort_cliMongoDB Find All, Sort commands, results in CLI
04_get_url_variableGet "FormValue" variable from URL
05_mongodb_crud_cliInsert, Update, Drop, Find, Index commands, results in CLI
06_ajax_send_receiveTwo Ajax examples: read and write
07_one_page_templateGO code and HTML template in one file
08_gowikiSimple wiki example
09_todo_list_htmlTodo list with struct and HTML template.
10_mongodbMongodb query results on HTML page in one go file.
11_socket_send_receiveSend/receive text between client/server via socket.
12_mgo_pipelineMongodb pipeline query saved in one go file
13_qr_barcodeQR code generator displays PNG image in browser.
14_read_txt_fileCompare ways to process TXT file
15_png_or_svg_barchartgenerate bar chart, PNG or SVG
16_drop_down_menu_formForm /w drop down menu, template with sub-templates
17_mysql_user_loginMySQL for user registration, login, and user list.
18_cookie_authenticationAuthentication with cookies.
19_os_global_variablesDisplaying all global system variables.
20_mongodb_crudMongodb CRUD with REST using httprouter & HTML templates
21_httprouter_templatehtml template with httprouter and ServeFiles
22_mongodb_crud_rest_htmlMongodb CRUD with REST using httprouter & HTML templates
23_file_uploaderUpload file and save on server
24_calculate_timeTime related features
25_https_static_filesServe HTTP and HTTPS w/ NotFound for static files
26_url_not_found_handlerCustom Not Found handler.
27_mongodb_bulk_upsertMongodb bulk insert from TXT file
28_markdownGenerate Markdown using blackfriday
29_go_crud_json_apiREST API using JSON with httprouter. JavaScript is used to view, create, edit, and delete records.
30_mongodb_crud_json_apiREST API using JSON, httprouter, and toml, i/o to MongoDB
31_send_emailSend email with attachment
32_colorful_cliCreate colorful CLI
33_testingTesting package example
34_channelsBuffered/unbuffered channels, forking channel, ranging over closed channel.
35_mongodb_pipeline_pageOne page MGO aggregation with pipeline
36_concurrency_channelChannel, waiting, concurrency, sleep, close, count, queue
37_html_templateSimple html template with Go
38_url_request_JSONConvert data to/from JSON, get and parse file from URL
39_read_directory_contentList files adn sub folders in a given folder
40_cron_schedulerSchedule processes
41_cli_argumentsRun cli utility with options using os.Args
42_upload_many_filesUpload multiple files from browser form to folder on a server.
43_resize_jpg_png_imageResize images
44_csv_fileRead and write to CSV file. Parse CSV file to slice of objects.
45_image_exif_dataGet image attributes for each image in a folder.
46_video_captureCapture video from web camera and display live.
47_download_slice_as_csvDownload link generates CSV or Tab Delimited file that can be saved localy on your computer.
48_keyboard_driverTesting IOT devices
49_constructorExample creating new package with allocation/constructor that accepts multiple types using interface. Experimenting with Readers and Writers.
50_golf_frameworkA fast, simple and lightweight micro-web framework for Go
51_blur_imageBlur, Rotate, and Generate Thumbnails.
52_jpg_image_watermarkAdd watermark to image
53_regular_expressionValidation, Find and replace, security
54_rotate_imageImage rotation in degrees from 1 to 360.
55_html_template_std_libHTML template using standard library packages
56_html_formatterWork in similar fashion as go fmt, but on HTML files.
57_valid_interfaceInterface as parameter.
58_GO_HTML_templateGo HTML template examples.
59_zip_and_unzipExample for archive/zip package.
60_http_response_as_fileHTTP handler responds with a copied file.
61_logging_middlewareSave logs and error logs to file or database.
62_download_progressProgress shown on CLI.
63_graphql_todo_exampleOne file GraphQL example.
64_
65_books_exampleMongoDB CRUD example.
66_server_sent_eventsLive logs (events) from server to browser using the EventSource HTML interface.
67_stringutilReverse a string, test included
68_iotaIota identifier is used in const declarations to simplify definitions of incrementing numbers.
69_JSONJSON input/output examples

The proper way to copy a slice

package main
import"fmt"funcmain() {
a:= []string{"a", "b", "c", "d"}
e:=make([]string, len(a))
copy(e, a)
fmt.Println(e)
}

How to find out the data type?

// Figure out what type it is: maps, slices, or arrays!package main
import (
"fmt""reflect"
)
funcmain() {
// Declaring local variablesmap1:=map[string]string{"name": "John", "desc": "Golang"}
map2:=map[string]int{"apple": 23, "tomato": 13}
slice1:= []int{1, 2, 3}
array1:= [3]int{1, 2, 3}
// var m map[string]int// m = make(map[string]int)// More info Here: https://blog.golang.org/go-maps-in-action// Type, such as map[string]string, []int, [3]intfmt.Println("map1:", reflect.TypeOf(map1))
fmt.Println("map2:", reflect.TypeOf(map2))
fmt.Println("slice1:", reflect.TypeOf(slice1))
fmt.Println("array1:", reflect.TypeOf(array1))
// Value, such as map, slice, array.fmt.Println("map1:", reflect.ValueOf(map1).Kind())
fmt.Println("map2:", reflect.ValueOf(map2).Kind())
fmt.Println("slice1:", reflect.ValueOf(slice1).Kind())
fmt.Println("array1:", reflect.ValueOf(array1).Kind())
// True/False statement inside Printffmt.Printf("%v is a map? %v\n", map1, reflect.ValueOf(map1).Kind() ==reflect.Map)
fmt.Printf("%v is a map? %v\n", map2, reflect.ValueOf(map2).Kind() ==reflect.Map)
fmt.Printf("%v is a map? %v\n", slice1, reflect.ValueOf(slice1).Kind() ==reflect.Map)
}

Go

In Go, the code does exactly what it says on the page.

It’s the simplicity that makes Go awesome.

Go: Statically typed yet expressive language with a focus on concurrency.

Go strives to keep things small and beautiful.

What I would have done in Python, Java, Ruby, PHP, C, C# or C++, I’m now doing in Go.

The code must be like a piece of music.

Code less, compile quicker, execute faster.

Any of your code that you haven’t looked at for 6 or more months may as well have been written by someone else.

General guideline: accept interfaces, return structs

Go is simple but not easy.

TODO :

  • MongoDB connection
  • MySQL connection
  • Resize Images
  • Set/Get Image tags
  • SQLite connection
  • Postgres connection
  • React frontend

About

I incorporate these code snippets into my larger programming modules.

Topics

Resources

Stars

40 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages