我正在做一個專案來顯示 json 資料并檢查它們是否是閏年。
我正在嘗試為IsLeapYear創建一個引數,以便我可以在進行測驗時傳入一些 json 值,我嘗試使用User指向結構的指標,以便我可以傳入函式引數中的欄位,但它沒有用.
如何在函式引數中傳入 user.json 中的 json 資料,以便我更輕松地進行測驗?
只是讓您知道,我的測驗檔案中出現錯誤,因為我創建的 DateTest 是字串,但IsLeapYear不是字串。
這是我的代碼:
main.go:
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"time"
)
// Users struct which contains
// an array of users
type Users struct {
Users []User `json:"users"`
}
// User struct which contains a name
// a type and a list of social links
type User struct {
Firstname string `json:"fname"`
Secondname string `json:"lname"`
Date string `json:"date"`
}
var users Users
func Birthday() {
// Open our jsonFile
jsonFile, err := os.Open("users.json")
// if we os.Open returns an error then handle it
if err != nil {
fmt.Println(err)
}
fmt.Println("Successfully Opened users.json")
// defer the closing of our jsonFile so that we can parse it later on
defer jsonFile.Close()
// read our opened xmlFile as a byte array.
byteValue, _ := ioutil.ReadAll(jsonFile)
// we initialize our Users array
// we unmarshal our byteArray which contains our
// jsonFile's content into 'users' which we defined above
json.Unmarshal(byteValue, &users)
IsLeapYear(&users{Firstname: ""}, &users{Secondname: ""}) ---> passing the users fileds
}
func IsLeapYear(firstname *Users, secondname *Users) {
// we iterate through every user within our users array and
// print out the user Type, their name
for i := 0; i < len(users.Users); i {
date, err := time.Parse("2006/01/02", users.Users[i].Date)
if err != nil {
date, err = time.Parse("2006-01-02", users.Users[i].Date)
// date, err = time.Parse("2006 01 02", users.Users[i].Date)
if err != nil {
log.Fatal("unsupported date format:", err)
}
}
// check if the date is a leap year, ex: 29 is not a leap year but 28th is !
if date.Day()%400 == 0 || (date.Day()%4 == 0 && date.Day()%100 != 0) {
fmt.Println("User Date: " users.Users[i].Date)
fmt.Println("User First Name: " users.Users[i].Firstname)
fmt.Println("User Second Name: " users.Users[i].Secondname)
fmt.Println(users.Users[i].Date, "is a Leap Year ? ? ?")
fmt.Println("TODAY IS NOT YOUR BIRTHDAY, but it's a leap year..!!! ?? ?? ?? ")
fmt.Println("")
// checking if the date.day matches today's date
if date.Day() == time.Now().Day() {
fmt.Println("User Date: " users.Users[i].Date)
fmt.Println("User First Name: " users.Users[i].Firstname)
fmt.Println("User Second Name: " users.Users[i].Secondname)
fmt.Println(users.Users[i].Date, "is a Leap Year ? ? ?")
fmt.Println("TODAY IS YOUR BIRTHDAY AND A Leap YEAR, Happy birthday !!!?? ?? ?? ?? ")
// not ur birthday today because the date in the json doesn't match todays date
}
} else {
fmt.Println("User Date: " users.Users[i].Date)
fmt.Println("User First Name: " users.Users[i].Firstname)
fmt.Println("User Second Name: " users.Users[i].Secondname)
fmt.Println(users.Users[i].Date, " is Not a Leap Year ?? ?? ?? ")
fmt.Println("Your Date is neither a leap year nor your birthday..!!! ??????")
}
}
}
func main() {
Birthday()
}
用戶.json:
{
"users": [
{
"Fname": "Johnny",
"Lname":"mane",
"date":"1982/01/08"
},
{
"Fname": "Wayne",
"Lname":"Bruce",
"date":"1965/01/30"
},
{
"Fname": "Gaga",
"Lname":"Lady",
"date":"1986/03/08"
},
{
"Fname": "radio",
"Lname":"head",
"date":"1988/02/29"
},
{
"Fname": "Mario",
"Lname":"torres",
"date":"1996/09/04"
},
{
"Fname": "robert",
"Lname":"Alex",
"date":"1991/12/05"
},
{
"Fname": "Julia",
"Lname":"sevak",
"date":"1991-03-28"
},
{
"Fname": "feb",
"Lname":"robert",
"date":"1995-05-24"
},
{
"Fname": "Liam",
"Lname":"Noah",
"date":"2002-10-04"
},
{
"Fname": "karim",
"Lname":"salim",
"date":"2021/10/21"
},
{
"Fname": "ahmed",
"Lname":"mario",
"date":"2021-10-24"
}
]
}
BD_test.go:
package main
import (
"testing"
)
type dateTest struct {
firstname, lastname, expected string
}
var DateTests = []dateTest{
{"karim", "salim", "2001/09/24"},
{"neno", "torres", "2001/14/44"},
{"harry", "potter", "2011/10/02"},
}
func TestIsLeap(t *testing.T) {
for _, test := range DateTests {
output := IsLeapYear(test.firstname, test.lastname). ---> error here !
expectedLeap := test.expected
if output != expectedLeap {
t.Errorf("Output %q not equal to expected %q", output, expectedLeap)
}
}
}
uj5u.com熱心網友回復:
在這里,我清理了你的代碼。現在測驗應該更容易寫了
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"time"
)
// Data struct which contains
// an array of users
type Data struct {
Users []User `json:"users"`
}
// User struct which contains the first last name and the birthdate
type User struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Date string `json:"date"`
}
func IsLeapYear(date time.Time) bool {
return date.Day()%400 == 0 || (date.Day()%4 == 0 && date.Day()%100 != 0)
}
func parseDate(date string) time.Time {
parsedDate, err := time.Parse("2006/01/02", date)
if err != nil {
parsedDate, err = time.Parse("2006-01-02", date)
if err != nil {
log.Fatal("unsupported date format:", err)
}
}
return parsedDate
}
func CheckBirthdateOf(user User) {
date := parseDate(user.Date)
fmt.Println("User Date: " user.Date)
fmt.Println("User First Name: " user.FirstName)
fmt.Println("User Last Name: " user.LastName)
if IsLeapYear(date) {
fmt.Println(user.Date, "is a Leap Year ? ? ?")
if date.Day() == time.Now().Day() {
fmt.Println("TODAY IS YOUR BIRTHDAY AND A Leap YEAR, Happy birthday !!!?? ?? ?? ?? ")
} else {
fmt.Println("TODAY IS NOT YOUR BIRTHDAY, but it's a leap year..!!! ?? ?? ?? ")
}
} else {
fmt.Println(user.Date, " is Not a Leap Year ?? ?? ?? ")
fmt.Println("Your Date is neither a leap year nor your birthday..!!! ??????")
}
fmt.Println("")
}
func main() {
jsonFile, err := os.Open("users.json")
if err != nil {
panic(err)
}
fmt.Println("Successfully Opened users_data.json")
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var usersData Data
json.Unmarshal(byteValue, &usersData)
for _, user := range usersData.Users {
CheckBirthdateOf(user)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/334854.html
