aider

package module
v0.0.0-...-c7b98d5 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2025 License: MIT Imports: 21 Imported by: 0

README

Tools and help

Documentation

Index

Constants

View Source
const (
	ErrNotFound     = 404
	ErrUnauthorized = 401
	ErrInternal     = 500
	ErrBadRequest   = 400
)

Define constants for error codes

Variables

This section is empty.

Functions

func CheckPassword

func CheckPassword(password, hashedPassword string) bool

CheckPassword ตรวจสอบว่ารหัสผ่านตรงกับรหัสผ่านที่เข้ารหัส

func CountDays

func CountDays(startDateStr, endDateStr string) (int, error)

นับวัน ระหว่างช่วงวันที่

func CreateMap

func CreateMap[K comparable, V any, T any](items []T, keyFunc func(T) K, valueFunc func(T) V) map[K]V

func CreateSlice

func CreateSlice[V any, T any](items []T, valueFunc func(T) V) []V

ฟังก์ชันที่สร้าง slice จาก struct ใด ๆ โดยกำหนดค่า value ที่จะนำมาใช้ใน slice นั้นได้

อธิบายโค้ด:

  1. ฟังก์ชัน CreateSlice: ฟังก์ชันนี้ใช้ generics เพื่อสร้าง slice จาก struct ใดๆ โดยกำหนดค่า value ที่จะนำมาใช้ใน slice: • V: ประเภทของ value. • T: ประเภทของ struct.
  2. ตัวอย่างโครงสร้างข้อมูล UserLogin และ Product: สร้าง struct UserLogin และ Product เพื่อใช้เป็นตัวอย่างข้อมูล.
  3. การใช้งานใน main function: • สร้าง slice ของ UserLogin และ Product. • ใช้ CreateSlice เพื่อสร้าง slice โดยกำหนดค่า value ที่จะนำมาใช้ใน slice: • สร้าง slice ที่มี UserName จาก UserLogin (userNames). • สร้าง slice ที่มี ProfileId จาก UserLogin (profileIds). • สร้าง slice ที่มี ProductName จาก Product (productNames). • สร้าง slice ที่มี Price จาก Product (productPrices).
  4. แสดงผลลัพธ์: พิมพ์ผลลัพธ์ของ slice ที่สร้างขึ้นเพื่อแสดงข้อมูลที่ถูกจัดเก็บใน slice.

โค้ดนี้ทำให้คุณสามารถสร้าง slice จาก struct ใดๆ ได้โดยกำหนดค่า value ตามที่ต้องการ, ทำให้มีความยืดหยุ่นในการใช้งาน.

func DD

func DD(values ...interface{})

func DDD

func DDD(v ...interface{}) (err error)

func Date

func Date(s string) time.Time

Date แปลงสตริงที่มีรูปแบบตามที่กำหนดให้เป็นค่าเวลา (time.Time) ฟังก์ชันนี้ใช้สำหรับการแปลงวันที่ในรูปแบบที่กำหนด (ตามตัวแปร `dateLayout`) ให้เป็นประเภท time.Time ถ้าแปลงสำเร็จ จะคืนค่าผลลัพธ์เป็นเวลา ในกรณีที่แปลงไม่สำเร็จ (เกิดข้อผิดพลาด) จะไม่คืนค่าผลลัพธ์ที่ถูกต้องเพราะเราละเว้นการจัดการข้อผิดพลาด dateLayout คืนค่าเฉพาะวันที่

func DateTime

func DateTime(s string) time.Time

datetimeLayout คืนค่า วันที่และเวลา

func DateTimeNow

func DateTimeNow() string

วันเวลา ปัจจุบัน ประเทศไทย แบบ string

func DateTimeToTime

func DateTimeToTime(datetime string) string

time.Time คืนค่าเฉพาะเวลา

func DaysBetween

func DaysBetween(a, b time.Time) int

คำนวณจำนวนวันระหว่างวันที่สอง (ระหว่าง a และ b)

func DecryptData

func DecryptData(cipheredStr, key string) (string, error)

ถอดรหัสข้อตวาม

func EncryptData

func EncryptData(plaintext, key string) (string, error)

เข้ารหัสข้อความ

func FormatDate

func FormatDate(dt *time.Time) (string, string)

FormatDate format date

func FormatDateTimeByPosition

func FormatDateTimeByPosition(dateTime, position string) string

ดึงข้อมูล วันที่ตามตำแหน่งที่กำหนด

func FormatISOToDatetime

func FormatISOToDatetime(datetime string) string

แปลง format วันที่ 2025-02-03T10:15:30Z TO 2025-02-03 10:15:3

func GenRedisKey

func GenRedisKey(v interface{}) string

แปลง struct หรือ ค่า ใดๆเป็น MD5 (ใช้สำหรับสร้าง key)

func GenerateJWT

func GenerateJWT[T any](jwtKey []byte, conFig JwtConfig, claimsStruct T) (string, error)

ฟังก์ชันสำหรับสร้าง JWT Token รองรับ struct ใดๆ GenerateJWT สร้าง JWT Token

func GetDate

func GetDate(date string) string

GetDate get date from string datetime format time.Time TO Feb ,กุมภาพันธ์

func GetDateTimeByDate

func GetDateTimeByDate(day time.Time) time.Time

GetDateTimeByDate get datetime by date timezone (th)

func GetNextDateTime

func GetNextDateTime(day int) time.Time

GetNextDateTime get datetime tomorrow timezone (th)

func GetPreviousDateTime

func GetPreviousDateTime(day int) time.Time

GetPreviousDateTime get datetime yesterday timezone (th)

func GetToday

func GetToday() time.Time

GetToday get datetime today timezone (th)

func GetYesterday

func GetYesterday() time.Time

GetYesterday get datetime yesterday timezone (th)

func HasKeyInMap

func HasKeyInMap[K comparable, T any](entities map[K]T, key K) bool

ฟังก์ชันนี้ตรวจสอบว่า map ที่มี key เป็น int หรือ string มี key ที่กำหนด หรือไม่

func HashPassword

func HashPassword(password string) (string, error)

HashPassword เข้ารหัสรหัสผ่านโดยใช้ bcrypt

func InSlice

func InSlice[T comparable](value T, array []T) bool

ตรวจสอบค่า ว่ามีใน Slice ไหม

func InTimeSpan

func InTimeSpan(start, end, check time.Time) bool

ตรวจสอบว่าเวลา check อยู่ในช่วงระหว่าง start และ end หรือไม่

func IsEncrypt

func IsEncrypt(key []byte, secure string) bool

ตรวจสอบ ความถูกต้องของรหัส

func IssetKeySlice

func IssetKeySlice[T any](entities []T, keyToCheck int) bool

ตรวจสอบว่า index ที่กำหนด อยู่ภายในขอบเขตของ slice หรือ ไม่

func JoinSlice

func JoinSlice[T any](slice []T, separator string) string

แปลง Slice ใดๆ เป็น string โดยสามารถกำหนดเครื่องหมายที่ใช้คั้นได้ (separator) เช่น , #

func MD5

func MD5(str string) string

เข้ารหัส MF5

func ModifyDatetime

func ModifyDatetime(datetime string, year, month, day, hour, min, sec int) string

ใช้สำหรับ บวก (เพิ่ม) หรือ ลบ (ลด) ค่าของ ปี, เดือน, วัน, ชั่วโมง, นาที และวินาที ไปยังวันที่ที่ระบุในรูปแบบของสตริง (datetime) และคืนค่าวันที่ที่ถูกปรับแล้วกลับมาในรูปแบบเดิม

func NewError

func NewError(code int, message string) error

NewError is a constructor function to create a new CustomError.

func PadZeros

func PadZeros(width int, number int) string

เติม 0 ด้านหน้าตัวเลข

func RandomNumber

func RandomNumber(min, max int) int

สุ่มตัวเลข

func RandomString

func RandomString(length int) string

สุ่ม string ตามจำนวนที่ต้องการ

func Round2d

func Round2d(value float64, d int32) float64

value=ค่าที่ต้องการ d=จำนวนทศนิยม

func ShortDate

func ShortDate(date time.Time, language string) string

ShortDate short date time.Time To 21 Feb. 2025 OR 21 ก.พ. 2568

func ShortMonth

func ShortMonth(date time.Time, language string) string

ShortYearMonth short month time.Tim TO Feb. 2025 , กุมภาพันธ์ 2025

func ShortYearMonth

func ShortYearMonth(date time.Time, language string) string

ShortYearMonth short month

func SplitString

func SplitString(str, separator string) []string

แปลง String เป็น []string โดยสามารถกำหนดเครื่องหมาย (separator) ที่ต้องการให้ตัดแบ่งได้ เช่น , #

func StringToFloat64

func StringToFloat64(value string) float64

แปลง string เป็น Float64

func StructToMapInterface

func StructToMapInterface(data interface{}) map[string]interface{}

ฟังก์ชันแปลง struct ใดๆ ให้เป็น map[string]interface{}

func TimeNowLocationTH

func TimeNowLocationTH() time.Time

TimeNowLocationTH get time location thai

func TimeTimeNow

func TimeTimeNow() time.Time

วันที่ปัจจุบัน ประเทศไทย แบบ time.Time

func ToByte

func ToByte(value string) []byte

แปลง String เป็น Byte

func ToFloat64

func ToFloat64(value interface{}) float64

แปลงค่าเป็น float64

func ToInt

func ToInt(value interface{}) int

แปลงค่าเป็น int

func ToMap

func ToMap[K comparable, T any](items []T, keyFunc func(T) K) map[K]T

ToMap แปลง slice ของ struct ใด ๆ ที่ใช้ key เป็นประเภทเดียวกันกับค่าที่ส่งเข้ามา K comparable: แทนที่ด้วย type ใด ๆ ก็ได้ที่สามารถใช้เป็น key ใน map (เช่น int, string, etc.) T any: แทนที่ด้วย type ใด ๆ ก็ได้ (Generics) items []T: slice ของ struct ที่ต้องการแปลง keyFunc func(T) K: ฟังก์ชันที่ใช้ในการดึง key จาก struct

อธิบายการทำงานของโค้ด:

  1. การประกาศฟังก์ชัน ToMap: • ฟังก์ชันนี้ใช้ Generics เพื่อให้สามารถทำงานกับ key และ value ที่เป็น type ใดก็ได้ • K comparable: กำหนดให้ K เป็นประเภทที่สามารถใช้เป็น key ใน map ได้ (ต้องสามารถเปรียบเทียบได้ เช่น int, string เป็นต้น) • T any: กำหนดให้ T เป็นประเภทใดก็ได้ • items []T: รับ slice ของ struct ที่ต้องการแปลง • keyFunc func(T) K: รับฟังก์ชันที่ใช้ในการดึง key จาก struct โดยคืนค่าเป็นประเภท K
  2. การสร้าง map: • สร้าง map ที่ชื่อ result โดยมี key เป็นประเภท K และ value เป็นประเภท T
  3. การวน loop ผ่านแต่ละ item ของ slice: • ใช้ for loop เพื่อวนผ่านแต่ละ item ใน slice items • ใช้ keyFunc เพื่อดึง key จาก item • นำ item ไปเก็บใน map result โดยใช้ key ที่ดึงมาได้
  4. การคืนค่า: • คืนค่า map ที่สร้างขึ้น

การใช้งาน:

ในฟังก์ชัน main:

  • สร้าง instance ของ struct User และกำหนดค่า field ต่าง ๆ
  • เรียกฟังก์ชัน ToMap เพื่อแปลง struct เป็น map โดยใช้ ID (ประเภท uint) เป็น key
  • เรียกฟังก์ชัน ToMap เพื่อแปลง struct เป็น map โดยใช้ Username (ประเภท string) เป็น key
  • พิมพ์ผลลัพธ์ที่ได้ออกมา

ผลลัพธ์จะเป็น map ที่มี key เป็นประเภทที่กำหนด (เช่น uint, string) และค่าของ map เป็น struct User ซึ่งสามารถใช้งานต่อไปได้ตามต้องการ

สร้าง map[ใดๆ]struct จาก struct ใดๆ

func ToString

func ToString(value interface{}) string

แปลงค่าเป็น string

func ToStringReflect

func ToStringReflect(value interface{}) string

ฟังก์ชันหลักที่ใช้แปลงค่าเป็น string โดยเรียกใช้ฟังก์ชันรองที่รองรับ recursive pointer ✅ รองรับหลายชนิดข้อมูล ✅ ป้องกัน recursive pointer ✅ แสดงข้อมูลในรูปแบบที่อ่านง่าย ✅ ใช้ reflect อย่างมีประสิทธิภาพ

func ToThaiMonth

func ToThaiMonth(month int) (string, error)

แปลงเดือนเป็นเดือนไทย

func ToTripleNestedMap

func ToTripleNestedMap[K1 comparable, K2 comparable, K3 comparable, T any](items []T, keyFunc1 func(T) K1, keyFunc2 func(T) K2, keyFunc3 func(T) K3) map[K1]map[K2]map[K3]T

ToTripleNestedMap แปลง slice ของ struct ใด ๆ ที่ใช้ key เป็นประเภทเดียวกันกับค่าที่ส่งเข้ามาเป็น map ซ้อนกันสามชั้น K1, K2, K3 comparable: แทนที่ด้วย type ใด ๆ ก็ได้ที่สามารถใช้เป็น key ใน map (เช่น int, string, etc.) T any: แทนที่ด้วย type ใด ๆ ก็ได้ (Generics) items []T: slice ของ struct ที่ต้องการแปลง keyFunc1, keyFunc2, keyFunc3 func(T) K: ฟังก์ชันที่ใช้ในการดึง key จาก struct

func ToUint64

func ToUint64(input interface{}) uint64

แปลงค่าเป็น uint64

func Trim

func Trim(v string) string

ใช้เพื่อลบช่องว่าง (whitespace) เช่น ช่องว่าง, แท็บ, และบรรทัดใหม่จากทั้งสองขอบของสตริง

func VerifyJWT

func VerifyJWT(jwtKey []byte, tokenString string) (*jwt.RegisteredClaims, error)

ฟังก์ชันสำหรับตรวจสอบ JWT Token

Types

type CustomError

type CustomError struct {
	Code    int
	Message string
}

CustomError defines a struct for handling error code and message.

func (*CustomError) Error

func (e *CustomError) Error() string

Implement the Error method to satisfy the error interface.

type JwtConfig

type JwtConfig struct {
	ExpirationTime int64  //เวลาหมดอายุของ JWT (แบบ Unix)
	Audience       string //ผู้รับที่ JWT นี้ถูกออกให้ ใคร ?
	Issuer         string //ผู้สร้าง JWT นี้
}

type TimeTime

type TimeTime struct {
	// contains filtered or unexported fields
}

Struct หลักที่เก็บเวลา

func TimeNow

func TimeNow() *TimeTime

ฟังก์ชันหลักสำหรับดึงวันเวลาปัจจุบัน

func (*TimeTime) DateOnly

func (d *TimeTime) DateOnly() string

เมธอดสำหรับดึงเฉพาะวันที่

func (*TimeTime) DateTime

func (d *TimeTime) DateTime() string

เมธอดสำหรับดึงวันเวลาเต็มรูปแบบ

func (*TimeTime) TimeOnly

func (d *TimeTime) TimeOnly() string

เมธอดสำหรับดึงเฉพาะเวลา

Jump to

Keyboard shortcuts

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