项目作者: xyproto

项目描述 :
:dango: Easy way to use a MariaDB/MySQL database from Go
高级语言: Go
项目地址: git://github.com/xyproto/simplemaria.git
创建时间: 2015-01-08T19:57:08Z
项目社区:https://github.com/xyproto/simplemaria

开源协议:MIT License

下载


SimpleMaria

GoDoc

An easy way to use a MariaDB/MySQL database from Go.

Online API Documentation

godoc.org

Features and limitations

  • Supports simple use of lists, hashmaps, sets and key/values.
  • Deals mainly with strings.
  • Uses the mysql package.
  • Modeled after simpleredis.
  • The hash maps behaves like hash maps, but are not backed by actual hashmaps, unlike with simpleredis. This is for keeping compatibility with simpleredis. If performance when scaling up is a concern, simpleredis combined with redis or Valkey might be a better choice.
  • MariaDB/MySQL normally has issues with variable size UTF-8 strings, even for for some combinations of characters. This package avoids these problems by compressing and hex encoding the data before storing in the database. This may slow down or speed up the time it takes to access the data, depending on your setup, but it’s a safe way to encode any string. This behavior is optional and can be disabled with host.SetRawUTF8(true), (to just use utf8mb4).

Sample usage

  1. package main
  2. import (
  3. "log"
  4. "github.com/xyproto/simplemaria"
  5. )
  6. func main() {
  7. // Check if the simplemaria service is up
  8. if err := db.TestConnection(); err != nil {
  9. log.Fatalln("Could not connect to local database. Is the service up and running?")
  10. }
  11. // Create a Host, connect to the local db server
  12. host := db.New()
  13. // Connecting to a different host/port
  14. //host := db.NewHost("server:3306/db")
  15. // Connect to a different db host/port, with a username and password
  16. // host := db.NewHost("username:password@server:port/db")
  17. // Close the connection when the function returns
  18. defer host.Close()
  19. // Create a list named "greetings"
  20. list, err := db.NewList(host, "greetings")
  21. if err != nil {
  22. log.Fatalln("Could not create list!")
  23. }
  24. // Add "hello" to the list, check if there are errors
  25. if list.Add("hello") != nil {
  26. log.Fatalln("Could not add an item to list!")
  27. }
  28. // Get the last item of the list
  29. if item, err := list.GetLast(); err != nil {
  30. log.Fatalln("Could not fetch the last item from the list!")
  31. } else {
  32. log.Println("The value of the stored item is:", item)
  33. }
  34. // Remove the list
  35. if list.Remove() != nil {
  36. log.Fatalln("Could not remove the list!")
  37. }
  38. }

Testing

A MariaDB/MySQL Database must be up and running locally for go test to work.

Version, license and author