{"id":18673054,"url":"https://github.com/vicanso/go-stringify","last_synced_at":"2025-04-12T01:31:23.288Z","repository":{"id":57521131,"uuid":"175826466","full_name":"vicanso/go-stringify","owner":"vicanso","description":"golang stringify function like javascript","archived":false,"fork":false,"pushed_at":"2019-03-20T12:50:00.000Z","size":16,"stargazers_count":8,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-25T21:36:05.987Z","etag":null,"topics":["go-stringify","stringify"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vicanso.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}},"created_at":"2019-03-15T13:35:10.000Z","updated_at":"2023-06-10T19:19:43.000Z","dependencies_parsed_at":"2022-09-26T18:01:06.424Z","dependency_job_id":null,"html_url":"https://github.com/vicanso/go-stringify","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicanso%2Fgo-stringify","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicanso%2Fgo-stringify/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicanso%2Fgo-stringify/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vicanso%2Fgo-stringify/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vicanso","download_url":"https://codeload.github.com/vicanso/go-stringify/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248504246,"owners_count":21115138,"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":["go-stringify","stringify"],"created_at":"2024-11-07T09:13:51.348Z","updated_at":"2025-04-12T01:31:23.050Z","avatar_url":"https://github.com/vicanso.png","language":"Go","readme":"# go-stringify\n\n[![Build Status](https://img.shields.io/travis/vicanso/go-stringify.svg?label=linux+build)](https://travis-ci.org/vicanso/go-stringify)\n\n\nStringify function for golang, it just like javascript's `JSON.stringify`. It supports customized replacement function, just like converting password to \"***\", cutting the string of which length is longer than 30.\n\n## API\n\n`String(interface{}, Replacer)`\n\n- `interface{}` data to json stringify \n- `Replacer` function for customized replacement\n\n```go\nReplacer func(key string, value interface{}) (replace bool, newValue string)\n```\n\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\tstringify \"github.com/vicanso/go-stringify\"\n)\n\ntype (\n\tOther struct {\n\t\tName        string                 `json:\"name,omitempty\"`\n\t\tAge         int                    `json:\"age,omitempty\"`\n\t\tVIP         bool                   `json:\"vip,omitempty\"`\n\t\tInformation map[string]interface{} `json:\"information,omitempty\"`\n\t}\n\tMe struct {\n\t\tID          string                 `json:\"id,omitempty\"`\n\t\tAccount     string                 `json:\"account,omitempty\"`\n\t\tPassword    string                 `json:\"password,omitempty\"`\n\t\tSecret      string                 `json:\"-\"`\n\t\tName        string                 `json:\"name,omitempty\"`\n\t\tAge         int                    `json:\"age,omitempty\"`\n\t\tVIP         bool                   `json:\"vip,omitempty\"`\n\t\tDetail      string                 `json:\"detail,omitempty\"`\n\t\tInformation map[string]interface{} `json:\"information,omitempty\"`\n\t\tBoss        Other                  `json:\"boss,omitempty\"`\n\t\tLeader      *Other                 `json:\"leader,omitempty\"`\n\t\tWorkmates   []Other                `json:\"workmates,omitempty\"`\n\t\tFriends     []*Other               `json:\"friends,omitempty\"`\n\t\tNil         []*Other               `json:\"nil,omitempty\"`\n\t}\n)\n\nfunc main() {\n\ts := getData()\n\t// {\"account\":\"t***t\",\"password\":\"***\",\"name\":\"foo\",\"age\":18,\"detail\":\"GitHub is where people build s...\",\"information\":{\"male\":true,\"bestFriend\":{\"name\":\"peter\"},\"fav\":\"reading\",\"weight\":70},\"boss\":{\"name\":\"boss\"},\"leader\":{\"age\":30},\"workmates\":[{\"name\":\"jack\"},{\"name\":\"tas\"}],\"friends\":[{\"name\":\"tom\"}]}\n\tfmt.Println(stringify.String(s, replacer))\n}\n\nfunc replacer(key string, value interface{}) (bool, string) {\n\t// password is hidden by ***\n\tif key == \"password\" {\n\t\treturn true, `\"***\"`\n\t}\n\t// mask the account\n\tif key == \"account\" {\n\t\tv := value.(string)\n\t\treturn true, `\"` + v[0:1] + \"***\" + v[len(v)-1:] + `\"`\n\t}\n\tstr, ok := value.(string)\n\t// cut the string\n\tif ok \u0026\u0026 len(str) \u003e 30 {\n\t\treturn true, `\"` + str[0:30] + `...\"`\n\t}\n\t// others not change\n\treturn false, \"\"\n}\n\nfunc getData() *Me {\n\tfriends := make([]*Other, 2)\n\tfriends = append(friends, \u0026Other{\n\t\tName: \"tom\",\n\t})\n\tpeter := \u0026Other{\n\t\tName: \"peter\",\n\t}\n\treturn \u0026Me{\n\t\tID:       \"\",\n\t\tAccount:  \"test\",\n\t\tPassword: \"pwd\",\n\t\tSecret:   \"none\",\n\t\tName:     \"foo\",\n\t\tAge:      18,\n\t\tVIP:      false,\n\t\tDetail:   \"GitHub is where people build software. More than 31 million people use GitHub to discover, fork, and contribute to over 100 million projects.\",\n\t\tInformation: map[string]interface{}{\n\t\t\t\"fav\":        \"reading\",\n\t\t\t\"weight\":     70,\n\t\t\t\"male\":       true,\n\t\t\t\"bestFriend\": peter,\n\t\t},\n\t\tBoss: Other{\n\t\t\tName: \"boss\",\n\t\t},\n\t\tLeader: \u0026Other{\n\t\t\tAge: 30,\n\t\t},\n\t\tWorkmates: []Other{\n\t\t\tOther{\n\t\t\t\tName: \"jack\",\n\t\t\t},\n\t\t\tOther{\n\t\t\t\tName: \"tas\",\n\t\t\t},\n\t\t},\n\t\tFriends: friends,\n\t}\n}\n```\n\n## benchmark\n\n```bash\ngoos: darwin\ngoarch: amd64\npkg: github.com/vicanso/go-stringify\nBenchmarkStringify-4   \t   50000\t     23350 ns/op\t    3320 B/op\t     144 allocs/op\nBenchmarkMarshal-4     \t  300000\t      5548 ns/op\t    1504 B/op\t      16 allocs/op\nPASS\nok  \tgithub.com/vicanso/go-stringify\t3.146s\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvicanso%2Fgo-stringify","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvicanso%2Fgo-stringify","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvicanso%2Fgo-stringify/lists"}