commit ec3da2d769e985c281a197acbbc0546072f13c38 Author: Rapturate Date: Mon Feb 23 09:46:28 2026 -0500 Gitea migration diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..37db34f --- /dev/null +++ b/LICENSE @@ -0,0 +1,18 @@ +MIT License + +Copyright (c) 2026 rapturate + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and +associated documentation files (the "Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the +following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial +portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT +LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO +EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..59699ff --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# API_Practice + +Practicing API creation in Golang \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..1138985 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module goPractice + +go 1.25.6 diff --git a/main.go b/main.go new file mode 100644 index 0000000..6fbddaf --- /dev/null +++ b/main.go @@ -0,0 +1,13 @@ +package main + +import ( + "fmt" + "goPractice/utils" +) + +func main() { + //Practicing Pulling and Printing data from an API + data := utils.PullData() + fmt.Print(utils.PrintUsers(data)) + fmt.Print(utils.GetBasicUserInfo(data)) +} diff --git a/utils/api_accessing.go b/utils/api_accessing.go new file mode 100644 index 0000000..e82c5b6 --- /dev/null +++ b/utils/api_accessing.go @@ -0,0 +1,70 @@ +package utils + +import ( + "encoding/json" + "fmt" + "log" + "net/http" +) + +// Building out the struct for storing the data from https://jsonplaceholder.typicode.com/users +type Users []struct { + ID int `json:"id"` + Name string `json:"name"` + Username string `json:"username"` + Email string `json:"email"` + Address struct { + Street string `json:"street"` + Suite string `json:"suite"` + City string `json:"city"` + Zipcode string `json:"zipcode"` + Geo struct { + Lat string `json:"lat"` + Lng string `json:"lng"` + } `json:"geo"` + } `json:"address"` + Phone string `json:"phone"` + Website string `json:"website"` + Company struct { + Name string `json:"name"` + CatchPhrase string `json:"catchPhrase"` + BS string `json:"bs"` + } `json:"company"` +} + +func PrintUsers(users Users) string { + var output string + for _, user := range users { + output += fmt.Sprintf("ID: %d\nName: %s\nUsername: %s\nEmail: %s\n", user.ID, user.Name, user.Username, user.Email) + output += fmt.Sprintf("\tAddress: \n\t%s, %s\n\t%s, %s\n", user.Address.Street, user.Address.Suite, user.Address.City, user.Address.Zipcode) + output += fmt.Sprintf("Geo Cooridnates:\n\tLatitude: %s\n\tLongitude: %s\n", user.Address.Geo.Lat, user.Address.Geo.Lng) + output += fmt.Sprintf("Phone: %s\nWebsite: %s\n", user.Phone, user.Website) + output += fmt.Sprintf("Company:\n\tCompany Name: %s\n\tCatchPhrase: %s\n\tBS: %s\n\n", user.Company.Name, user.Company.CatchPhrase, user.Company.BS) + } + return output +} + +func GetBasicUserInfo(users Users) string { + var output string + for _, user := range users { + output += fmt.Sprintf("User ID: %d\nName: %s\nUsername: %s\nEmail: %s\n\n", user.ID, user.Name, user.Username, user.Email) + } + return output +} + +func PullData() Users { + rawData, err := http.Get("https://jsonplaceholder.typicode.com/users") + if err != nil { + log.Fatal(err) + } + defer rawData.Body.Close() + + var users Users + + err = json.NewDecoder(rawData.Body).Decode(&users) + if err != nil { + log.Fatal(err) + } + + return users +}