Gitea migration
This commit is contained in:
18
LICENSE
Normal file
18
LICENSE
Normal file
@@ -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.
|
||||
13
main.go
Normal file
13
main.go
Normal file
@@ -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))
|
||||
}
|
||||
70
utils/api_accessing.go
Normal file
70
utils/api_accessing.go
Normal file
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user