मैं अपने उबंटू 12.04.1 लैपटॉप पर 1.0.3 चला रहा हूं और मैंने एक समस्या पर ठोकर खाई है जहां मैं मुख्य() में कुछ कोड चलाता हूं, यह बहुत व्यवहार करता है अगर मैं इसे परीक्षण के साथ चलाता हूं तो उससे अलग तरीके से।गो कोड अलग-अलग परीक्षण में बना रहता है
यहाँ मेरी उदाहरण है:
main.go
package main
import (
"image"
"image/jpeg"
"fmt"
"myproj/htmlutil"
[some imports removed]
)
func main() {
img, err := htmlutil.GetResizedImageFromWeb("http://img.foodnetwork.com/FOOD/2011/05/04/FNM_060111-OOT-B005_s4x3.jpg")
if err != nil {
fmt.Println("There was a problem ",err)
}
fmt.Println("Bounds were ",img.Bounds())
}
से myproj/htmlutil_test.go
package htmlutil
import (
"image"
"fmt"
"testing"
[some imports removed]
)
func TestGetImageFromURL(t *testing.T){
img, err := GetResizedImageFromWeb("http://img.foodnetwork.com/FOOD/2011/05/04/FNM_060111-OOT-B005_s4x3.jpg")
if err != nil {
t.Fatalf("There was a problem %q",err)
}
fmt.Println("Bounds were ",img.Bounds())
}
से और समारोह है कि वे कहते हैं, GetResizedImageFromWeb(), myproj/htmlutil में है .go:
package htmlutil
import (
"errors"
"fmt"
"image"
"io/ioutil"
"net/http"
[some imports removed]
)
func GetResizedImageFromWeb(imageURL string) (image.Image, error) {
resp, err := http.Get(imageURL)
if err != nil {
return nil, errors.New(fmt.Sprint("There was a problem reading the site %q Debug[%s]",imageURL, err))
}
defer resp.Body.Close()
//Decode the image using image's general purpose decoder
image, s, err := image.Decode(resp.Body)
if err != nil {
return nil, err
}
return resizeImage(image), nil
}
जब मैं "go run main.go" चलाता हूं कमांड लाइन, मैं यूआरएल से छवि की सीमाएं देखता हूं और इसे डिस्क पर एक jpg फ़ाइल के रूप में सहेज सकता हूं अगर मैं main.go में किसी फ़ंक्शन का उपयोग करना चाहता हूं।
There was a problem "image: unknown format"
क्या केवल इकाई परीक्षण में विफल समस्या का कारण है: हालांकि, जब मैं चलाने htmlutil पैकेज, मैं निम्नलिखित त्रुटि मिलती है से "परीक्षण जाना"? मैं क्या गलत कर रहा हूं?
मेरा एकमात्र अनुमान यह है कि किसी भी कारण से, html.Get() परीक्षण परिदृश्य में सभी डेटा वापस नहीं कर रहा है, लेकिन मैं अभी भी परेशान हूं कि ऐसा क्यों होता है।