जाओ
उत्तर
पहले (के रूप में एक पुराने जवाब में बताया गया) "आसान" यह करने के लिए जिस तरह से तीसरे पक्ष संकुल कि सीजीओ की जरूरत है और iconv पुस्तकालय लिपटे का उपयोग कर शामिल किया गया। यह कई कारणों से अवांछनीय है। शुक्र है, काफी समय से गो लेखकों द्वारा प्रदान किए गए केवल पैकेजों का उपयोग करके ऐसा करने का एक बेहतर तरीका है (संकुल के मुख्य सेट में नहीं बल्कि Go Sub-Repositories में)।
golang.org/x/text/encoding
पैकेज सामान्य वर्ण एन्कोडिंग कि UTF-8 से/करने के लिए परिवर्तित कर सकते हैं के लिए एक इंटरफेस को परिभाषित करता है। golang.org/x/text/encoding/simplifiedchinese
उप-पैकेज GB18030, GBK और HZ-GB2312 एन्कोडिंग कार्यान्वयन प्रदान करता है।
यहां एक जीबीके एन्कोडेड फ़ाइल पढ़ने और लिखने का एक उदाहरण है। ध्यान दें कि io.Reader
और io.Writer
डेटा को "फ्लाई पर" एन्कोडिंग करें क्योंकि डेटा पढ़ा/लिखा जा रहा है।
package main
import (
"bufio"
"fmt"
"log"
"os"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
// Encoding to use. Since this implements the encoding.Encoding
// interface from golang.org/x/text/encoding you can trivially
// change this out for any of the other implemented encoders,
// e.g. `traditionalchinese.Big5`, `charmap.Windows1252`,
// `korean.EUCKR`, etc.
var enc = simplifiedchinese.GBK
func main() {
const filename = "example_GBK_file"
exampleWriteGBK(filename)
exampleReadGBK(filename)
}
func exampleReadGBK(filename string) {
// Read UTF-8 from a GBK encoded file.
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
r := transform.NewReader(f, enc.NewDecoder())
// Read converted UTF-8 from `r` as needed.
// As an example we'll read line-by-line showing what was read:
sc := bufio.NewScanner(r)
for sc.Scan() {
fmt.Printf("Read line: %s\n", sc.Bytes())
}
if err = sc.Err(); err != nil {
log.Fatal(err)
}
if err = f.Close(); err != nil {
log.Fatal(err)
}
}
func exampleWriteGBK(filename string) {
// Write UTF-8 to a GBK encoded file.
f, err := os.Create(filename)
if err != nil {
log.Fatal(err)
}
w := transform.NewWriter(f, enc.NewEncoder())
// Write UTF-8 to `w` as desired.
// As an example we'll write some text from the Wikipedia
// GBK page that includes Chinese.
_, err = fmt.Fprintln(w,
`In 1995, China National Information Technology Standardization
Technical Committee set down the Chinese Internal Code Specification
(Chinese: 汉字内码扩展规范(GBK); pinyin: Hànzì Nèimǎ
Kuòzhǎn Guīfàn (GBK)), Version 1.0, known as GBK 1.0, which is a
slight extension of Codepage 936. The newly added 95 characters were not
found in GB 13000.1-1993, and were provisionally assigned Unicode PUA
code points.`)
if err != nil {
log.Fatal(err)
}
if err = f.Close(); err != nil {
log.Fatal(err)
}
}
go-iconv आज़माएं। यह iconv
लपेटता है और io.Reader
और io.Writer
लागू करता है।
गोलांग-चीन चर्चा समूह में यह messagego-iconv
उपयोग के कुछ उदाहरणों का उल्लेख कर रहा है।
एक और विकल्प फ़ाइल को एक बफर में एक अपारदर्शी ब्लॉब के रूप में पढ़ना होगा और फिर इसे किसी विशेष एन्कोडिंग में टेक्स्ट के रूप में समझना होगा [go-charset] का उपयोग कर (http://code.google.com/p/go-charset); अधिक जानकारी के लिए [यह सवाल] देखें (http://stackoverflow.com/q/10039701/720999)। मैंने कोड को नहीं देखा, लेकिन ऐसा लगता है कि सबसे अधिक उपयोग किए जाने वाले विरासत वर्णमाला/एन्कोडिंग के सेट का समर्थन करने के लिए, इस पैकेज को आइकनव की आवश्यकता नहीं है जो एक लाभ हो सकता है। – kostix
@kostix http://code.google.com/p/go-charset के शीर्षक पृष्ठ पर जानकारी से ऐसा लगता है कि gbk के मामले में पैकेज GNU iconv लाइब्रेरी का उपयोग करता है। –
इस पैकेज का उपयोग रूपांतरण करने के लिए भी किया जा सकता है: https://godoc.org/golang.org/x/text/encoding – OscarRyz
- 1. निम्नलिखित जाओ
- 2. जाओ IGrouping
- 3. जाओ समारोह
- 4. जाओ लॉकर
- 5. जाओ सामान्य
- 6. संलग्न जाओ
- 7. जाओ: सॉकेट
- 8. जाओ: अनन्य
- 9. एसक्यूएलएक्सप्शन: 'जाओ'
- 10. वर्तमान चयनित विकल्प जाओ
- 11. फेसबुक ग्राफ एपीआई: जाओ
- 12. जाओ पिछले mysql2 मणि
- 13. उदाहरण जाओ सशर्त StructureMap
- 14. हो जाओ नोड उदाहरण
- 15. जाओ सरणी टुकड़ा
- 16. बिल्ड और जाओ
- 17. जाओ पिछला दिन
- 18. जाओ इंटरैक्टिव खोल
- 19. सी # जाओ SettingsPropertyCollection
- 20. जाओ सामान्य प्रकार टी
- 21. Regex जाओ प्रोग्रामिंग भाषा
- 22. एक जाओ टेम्पलेट
- 23. जाओ विवरण कक्षा स्तर
- 24. मार्ग पथ जाओ गतिशील
- 25. वर्तमान समय जाओ
- 26. हो जाओ प्रत्येक सूची
- 27. हो जाओ PropertyInfo मूल्य
- 28. फाइल एक्सटेंशन (basename?) जाओ
- 29. वर्तमान डोमेन हो जाओ
- 30. पिछले हो जाओ सी #
क्यों downvotes? –
यह एक बहुत ही उचित और स्पष्ट प्रश्न जैसा दिखता है, फिर से खोलने के लिए मतदान (इसके मूल रूप में भी) – OscarRyz