{"id":15440517,"url":"https://github.com/glassonion1/xgo","last_synced_at":"2025-04-19T18:26:50.571Z","repository":{"id":57567725,"uuid":"339678318","full_name":"glassonion1/xgo","owner":"glassonion1","description":"The xgo contains various useful functions, such as DeepCopy, Retry etc.","archived":false,"fork":false,"pushed_at":"2024-09-12T05:11:56.000Z","size":96,"stargazers_count":11,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-29T11:41:32.312Z","etag":null,"topics":["deepcopy","go","golang","retry","utility-library"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/glassonion1.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-02-17T09:56:48.000Z","updated_at":"2024-09-28T04:47:31.000Z","dependencies_parsed_at":"2024-09-12T13:47:40.064Z","dependency_job_id":"1f874b20-761f-4806-94d9-3f48685bad29","html_url":"https://github.com/glassonion1/xgo","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/glassonion1%2Fxgo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/glassonion1%2Fxgo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/glassonion1%2Fxgo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/glassonion1%2Fxgo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/glassonion1","download_url":"https://codeload.github.com/glassonion1/xgo/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249239239,"owners_count":21235821,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deepcopy","go","golang","retry","utility-library"],"created_at":"2024-10-01T19:13:55.360Z","updated_at":"2025-04-16T12:30:42.013Z","avatar_url":"https://github.com/glassonion1.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# xgo - Useful Go library\n\n[![Test CLI](https://github.com/glassonion1/xgo/actions/workflows/test.yml/badge.svg)](https://github.com/glassonion1/xgo/actions/workflows/test.yml)\n[![Godoc](https://img.shields.io/badge/godoc-reference-blue)](https://godoc.org/github.com/glassonion1/xgo)\n[![Go Report Card](https://goreportcard.com/badge/github.com/glassonion1/xgo)](https://goreportcard.com/report/github.com/glassonion1/xgo)\n[![GitHub license](https://img.shields.io/github/license/glassonion1/xgo)](https://github.com/glassonion1/xgo/blob/main/LICENSE)\n\nThe xgo contains various useful features for gohers.\n\n## Features\n- Deep copy\n- Contains\n- Chunk\n- Exponential backoff\n- Struct to map\n- Obtain pointers to types\n\n### Protobuf support\nThere is an extension package that supports deep copying Protobuf timestamp types.\n- [xgo pb](https://github.com/glassonion1/xgo/tree/main/xgopb)\n\n## Install\n```\n$ go get github.com/glassonion1/xgo\n```\n\n## Import\n```go\nimport \"github.com/glassonion1/xgo\"\n```\n\n## Usage\n### Deep copy\nfield-to-field copying based on matching names.\n\nIn layered architecture, there are times when we need to copy values between structs that have different types but share the same field names. \nThe DeepCopy function is designed to be used in such scenarios.\n\nSupport for copying data:\n- from struct to struct\n- from struct to pointer\n- from pointer to struct\n- from slice to slice\n#### from struct to struct\n```go\npackage xgo_test\n\nimport (\n    \"fmt\"\n    \"time\"\n\n    \"github.com/glassonion1/xgo\"\n)\n\ntype FromModel struct {\n    ID         string `copier:\"Id\"`\n    Name       string\n    CreatedAt  time.Time\n    UpdatedAt  *time.Time\n}\ntype ToModel struct {\n    Id         string\n    Name       string\n    CreatedAt  time.Time\n    UpdatedAt  *time.Time\n}\n\nfunc Example() {\n    now := time.Date(2025, 6, 1, 0, 0, 0, 0, time.UTC)\n    from := FromModel{\n        ID:        \"xxxx\",\n        Name:      \"R2D2\",\n        CreatedAt: now,\n        UpdatedAt: \u0026now,\n    }\n    to := \u0026ToModel{}\n    err := xgo.DeepCopy(from, to)\n    if err != nil {\n        // handles error\n    }\n    fmt.Println(\"ToModel object:\", to)\n\n    // Output: ToModel object: \u0026{xxxx R2D2 2025-06-01 00:00:00 +0000 UTC 2025-06-01 00:00:00 +0000 UTC}\n}\n```\n\n#### from slice to slice\n```go\ntype FromModel struct {\n    ID         string\n    Name       string\n}\ntype ToModel struct {\n    ID         string\n    Name       string\n}\n\nfunc Example() {\n    from := []FromModel{\n        {\n            ID: \"xxxx1\",\n            Name: \"R2D2\",\n        },\n    {\n            ID: \"xxxx2\",\n            Name: \"C3PO\",\n        },\n    }\n    to := \u0026[]ToModel{}\n    err := xgo.DeepCopy(from, to)\n    if err != nil {\n        // handles error\n    }\n    fmt.Println(\"ToModel object:\", to)\n    \n    // Output: ToModel object: \u0026[{xxxx1 R2D2} {xxxx2 C3PO}]\n}\n```\n\n### Contains\nContains method for a slice.\n```go\n// slice of int32\ncontainsInt32 := xgo.Contains([]int32{1, 2, 3, 4, 5}, 3)\nfmt.Println(\"contains int32:\", containsInt32)\n\n// slice of int\ncontainsInt := xgo.Contains([]int{1, 2, 3, 4, 5}, 2)\nfmt.Println(\"contains int:\", containsInt)\n\n// slice of float64\ncontainsFloat64 := xgo.Contains([]float64{1.1, 2.2, 3.3, 4.4, 5.5}, 4.4)\nfmt.Println(\"contains float64:\", containsFloat64)\n\n// slice of string\ncontainsString := xgo.Contains([]string{\"r2d2\", \"c3po\", \"bb8\"}, \"c3po\")\nfmt.Println(containsString) // -\u003e true\n\n// slice of struct\ntype hero struct {\n    ID   string\n    Name string\n}\nlist := []hero{\n    hero{\n        ID:   \"1\",\n        Name: \"Luke Skywalker\",\n    },\n    hero{\n        ID:   \"2\",\n        Name: \"Han Solo\",\n    },\n    hero{\n        ID:   \"3\",\n        Name: \"Leia Organa\",\n    },\n}\ntarget := hero{\n\tID:   \"2\",\n\tName: \"Han Solo\",\n}\ncontainsStruct := xgo.Contains(list, target)\nfmt.Println(\"contains struct:\", containsStruct)\n\n// Output:\n// contains int32: true\n// contains int: true\n// contains float64: true\n// contains string: true\n// contains struct: true\n```\n\n### ToPtr\nObtain pointers to types\n```go\ntype Vegetables string\n\nconst (\n\tPea     Vegetables = \"Pea\"\n\tOkra    Vegetables = \"Okra\"\n\tPumpkin Vegetables = \"Pumpkin\"\n)\n\ntype Model struct {\n\tID        *int\n\tName      *string\n\tMaterial  *Vegetables\n\tCreatedAt *time.Time\n}\n\nfunc ExampleToPtr() {\n\tobj := Model{\n\t\tID:        xgo.ToPtr(123),\n\t\tName:      xgo.ToPtr(\"R2D2\"),\n\t\tMaterial:  xgo.ToPtr(Pea),\n\t\tCreatedAt: xgo.ToPtr(time.Date(2020, 6, 1, 0, 0, 0, 0, time.UTC)),\n\t}\n\tfmt.Println(\"object:\", obj)\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fglassonion1%2Fxgo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fglassonion1%2Fxgo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fglassonion1%2Fxgo/lists"}