ghelper

module
v1.0.2-0...-bc43c14 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2026 License: MIT

README

ghelper

Go Reference Go Report Card CI

A production-ready Go utility library providing common helpers for type conversion, HTTP requests, ID generation, file operations, string manipulation, IP extraction, Markdown rendering, and OS detection.

Installation

go get github.com/dengmengmian/ghelper

Requires Go 1.21+.

Packages

gconvert — Type Conversion

Powerful and easy-to-use converting functions for any types.

import "github.com/dengmengmian/ghelper/gconvert"

// Basic conversions s := gconvert.String(42) // "42" i := gconvert.Int("123") // 123 b := gconvert.Bool(1) // true f := gconvert.Float64("3.14") // 3.14

// Slice conversions strs := gconvert.Strings([]int{1, 2, 3}) // []string{"1", "2", "3"} ints := gconvert.Ints([]string{"4", "5", "6"}) // []int{4, 5, 6}

// Runtime type conversion v := gconvert.Convert("42", "int") // 42 (as interface{})

// Utilities gconvert.IsEmpty(nil) // true gconvert.IsNumeric("123.456") // true gconvert.UcFirst("hello") // "Hello"

gstring — String Utilities

import "github.com/dengmengmian/ghelper/gstring"

m, err := gstring.StringToJson({"name":"test","value":42}) // m["name"] == "test", m["value"] == float64(42)

gstring.IsEmail([]byte("user@example.com")) // true gstring.GetRandomString(10) // e.g. "aB3xK9mQ2w" gstring.Md5([]byte("hello")) // "5d41402abc4b2a76b9719d911017c592"

gid — Unique ID Generation

import "github.com/dengmengmian/ghelper/gid"

// Short 6-character unique ID (mixed-case alphanumeric) id, err := gid.GenShortID() // e.g. "a1B2cD" id, err = gid.GenShortID(gid.WithNumber(8)) // 8-character id, err = gid.GenShortID(gid.WithStartWithYear(true)) // prefixed with year if err != nil { // handle error }

// Distributed unique ID (20 chars, based on xid) uuid := gid.UUID() // e.g. "cps4qlt91vg0p4h4qog0"

// Order number with timestamp orderNum := gid.FetchOrderNum(42) // "2301011200000000421234"

ghttp — HTTP Client

A feature-rich HTTP request library with support for headers, cookies, query params, timeout, proxy, compression, and more.

import "github.com/dengmengmian/ghelper/ghttp"

// Simple GET resp, err := ghttp.Request{ Url: "https://api.example.com/data", Method: "GET", }.Do() if err != nil { // handle error } defer resp.Body.Close() body, _ := resp.Body.FromToString()

// POST JSON with query params, headers, and cookies type User struct { Name string json:"name" Age int json:"age" } resp, err := ghttp.Request{ Url: "https://api.example.com/users", Method: "POST", Query: User{Name: "test"}, ContentType: "application/json", UserAgent: "my-app/1.0", Timeout: 5 * time.Second, Body: map[string]string{"message": "hello"}, }.WithCookie(&http.Cookie{Name: "session", Value: "abc"}).Do()

// Parse JSON response var result MyStruct resp.Body.FromToJson(&result)

// With proxy and basic auth resp, err := ghttp.Request{ Url: "https://api.example.com/secure", Proxy: "http://proxy:8080", BasicAuthUsername: "admin", BasicAuthPassword: "secret", }.Do()

// Gzip compression resp, err := ghttp.Request{ Method: "POST", Url: "https://api.example.com/upload", Compression: ghttp.Gzip(), Body: largePayload, }.Do()

gfile — File Operations

import "github.com/dengmengmian/ghelper/gfile"

gfile.GetExt("image.png") // ".png" gfile.CheckNotExist("/some/path") // true if path does not exist gfile.IsDir("/tmp") // true gfile.MkDir("/tmp/myapp") // create directory gfile.IsNotExistMkDir("/tmp/myapp") // create if not exists gfile.CopyFile("src.txt", "dst.txt") // copy file gfile.CopyDir("src_dir", "dst_dir") // recursive directory copy

gip — Client IP Extraction

Extracts the client IP from an HTTP request, handling proxies and load balancers.

import "github.com/dengmengmian/ghelper/gip"

func handler(w http.ResponseWriter, r *http.Request) { ip := gip.GetIp(r) // Reads X-Forwarded-For → X-Real-Ip → RemoteAddr }

gmarkdown — Markdown to HTML

import "github.com/dengmengmian/ghelper/gmarkdown"

html := gmarkdown.MdToHTML("# Hello\n\nbold and italic") //

Hello

bold and italic

gos — OS Utilities

import "github.com/dengmengmian/ghelper/gos"

gos.IsWindows() // true on Windows gos.IsLinux() // true on Linux gos.IsDarwin() // true on macOS gos.Pwd() // current working directory gos.Home() // user home directory

Development

Build all packages

go build ./...

Run tests with race detector

go test -race ./...

Run vet

go vet ./...

License

MIT © dengmengmian

Directories

Path Synopsis
Package gconv implements powerful and easy-to-use converting functionality for any types of variables.
Package gconv implements powerful and easy-to-use converting functionality for any types of variables.
Package gfile provides file system utility functions such as copying, directory creation, permission checking, and path handling.
Package gfile provides file system utility functions such as copying, directory creation, permission checking, and path handling.
Package ghttp provides an HTTP client with support for custom headers, cookies, timeout, proxy, TLS configuration, compression, and debugging.
Package ghttp provides an HTTP client with support for custom headers, cookies, timeout, proxy, TLS configuration, compression, and debugging.
Package gid provides unique ID generation utilities including short IDs, UUIDs, and order numbers.
Package gid provides unique ID generation utilities including short IDs, UUIDs, and order numbers.
Package gip provides client IP extraction from HTTP requests, respecting common reverse-proxy headers such as X-Forwarded-For and X-Real-IP.
Package gip provides client IP extraction from HTTP requests, respecting common reverse-proxy headers such as X-Forwarded-For and X-Real-IP.
Package gmarkdown converts Markdown content to HTML with CommonMark extensions and configurable renderer options.
Package gmarkdown converts Markdown content to HTML with CommonMark extensions and configurable renderer options.
Package gos provides operating-system detection and user-home directory resolution across Windows, Linux, and macOS.
Package gos provides operating-system detection and user-home directory resolution across Windows, Linux, and macOS.
Package gstring provides common string utilities including JSON conversion, email validation, random string generation, and MD5 hashing.
Package gstring provides common string utilities including JSON conversion, email validation, random string generation, and MD5 hashing.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL