aider

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

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

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddDatetime

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

AddDatetime add date time only format 2006-01-02 15:04:05

func CheckPassword

func CheckPassword(password, hashedPassword string) bool

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

func ConvertDateTime

func ConvertDateTime(dateTime, position string) string

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

func DateTime

func DateTime(s string) time.Time

func DateTimeNow

func DateTimeNow() string

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

func DateTimeToDateTime

func DateTimeToDateTime(datetime string) string

GetDate get date from string datetime format

func DateTimeToTime

func DateTimeToTime(datetime string) string

func DaysBetween

func DaysBetween(a, b time.Time) int

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 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

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 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

func IsEncrypt

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

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

func IssetKeySlice

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

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

func IssetMapKyeInt

func IssetMapKyeInt[T comparable](entities map[int]T, k int) bool

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

func IssetMapKyeString

func IssetMapKyeString[T comparable](entities map[string]T, k string) bool

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

func JoinSlice

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

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

func MD5

func MD5(str string) string

เข้ารหัส MF5

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 ReDatetime

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

ReduceDatetime reduce re date time only format 2006-01-02 15:04:05

func Round2d

func Round2d(value float64, d int32) float64

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

func ShortDate

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

ShortDate short date

func ShortMonth

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

ShortYearMonth short month

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 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 VerifyJWT

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

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

Types

type JwtConfig

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

Jump to

Keyboard shortcuts

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