第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在函數(shù)中將結(jié)構(gòu)字段作為參數(shù)傳遞以顯示 json 數(shù)據(jù)?

如何在函數(shù)中將結(jié)構(gòu)字段作為參數(shù)傳遞以顯示 json 數(shù)據(jù)?

Go
紫衣仙女 2022-10-10 15:52:39
我正在做一個項目來顯示 json 數(shù)據(jù)并檢查它們是否是閏年。我正在嘗試為IsLeapYear創(chuàng)建一個參數(shù),以便在進(jìn)行測試時可以傳入一些 json 值,我嘗試使用User指向結(jié)構(gòu)的指針,以便可以傳入函數(shù)參數(shù)中的字段,但它不起作用.如何在函數(shù)參數(shù)中傳遞來自 user.json 的 json 數(shù)據(jù),這樣我可以更輕松地進(jìn)行測試?只是為了讓您知道,我的測試文件中出現(xiàn)錯誤,因為我創(chuàng)建了 DateTest 字符串,但I(xiàn)sLeapYear不是字符串。這是我的代碼:main.go:  package mainimport (    "encoding/json"    "fmt"    "io/ioutil"    "log"    "os"    "time")// Users struct which contains// an array of userstype Users struct {    Users []User `json:"users"`}// User struct which contains a name// a type and a list of social linkstype User struct {    Firstname  string `json:"fname"`    Secondname string `json:"lname"`    Date       string `json:"date"`}var users Usersfunc 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)            }        }
查看完整描述

1 回答

?
拉風(fēng)的咖菲貓

TA貢獻(xiàn)1995條經(jīng)驗 獲得超2個贊

在這里,我清理了你的代碼。現(xiàn)在測試應(yīng)該更容易編寫

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)

    }

}


查看完整回答
反對 回復(fù) 2022-10-10
  • 1 回答
  • 0 關(guān)注
  • 83 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號