खोज…


स्टाफ़ के अतिरिक्त नामानिगार

fmt.Stringer इंटरफ़ेस को संतुष्ट होने के लिए एक एकल विधि, String() string आवश्यकता होती है। स्ट्रिंग विधि उस मूल्य के लिए "मूल" स्ट्रिंग प्रारूप को परिभाषित करती है, और डिफ़ॉल्ट प्रतिनिधित्व है यदि मूल्य किसी भी fmt संकुल स्वरूपण या मुद्रण दिनचर्या को प्रदान किया जाता है।

package main

import (
    "fmt"
)

type User struct {
    Name  string
    Email string
}

// String satisfies the fmt.Stringer interface for the User type
func (u User) String() string {
    return fmt.Sprintf("%s <%s>", u.Name, u.Email)
}

func main() {
    u := User{
        Name:  "John Doe",
        Email: "[email protected]",
    }

    fmt.Println(u)
    // output: John Doe <[email protected]>
}

Playground

बेसिक एफ.एम.टी.

पैकेज fmt लागू प्रारूप I / O प्रारूप क्रिया का उपयोग करके स्वरूपित किया गया:

%v    // the value in a default format
%T    // a Go-syntax representation of the type of the value
%s    // the uninterpreted bytes of the string or slice

प्रारूप कार्य

fmt में 4 मुख्य प्रकार के फ़ंक्शन और भीतर कई बदलाव हैं।

छाप

fmt.Print("Hello World")        // prints: Hello World
fmt.Println("Hello World")      // prints: Hello World\n
fmt.Printf("Hello %s", "World") // prints: Hello World

पूरे वेग से दौड़ना

formattedString := fmt.Sprintf("%v %s", 2, "words") // returns string "2 words"

Fprint

byteCount, err := fmt.Fprint(w, "Hello World") // writes to io.Writer w

http हैंडलर के अंदर, Fprint का उपयोग किया जा सकता है:

func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello %s!", "Browser")
}   // Writes: "Hello Browser!" onto http response

स्कैन

स्कैन स्कैन पाठ मानक इनपुट से पढ़ा जाता है।

var s string
fmt.Scanln(&s) // pass pointer to buffer
// Scanln is similar to fmt.Scan(), but it stops scanning at new line.
fmt.Println(s) // whatever was inputted

स्ट्रिंगर इंटरफ़ेस

कोई भी मान जिसमें एक String() विधि fmt पूर्णांक Stringer लागू करता है

type Stringer interface {
        String() string
}


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow