如果使用import func,如何在控制台中获取value属性?

时间:2018-05-17 12:43:06

标签: go yaml

我需要获得财产价值:

  • telegram_token:" telegramtoken"
  • other_token:" othertoken"

但是如果我Init() import api func main()并在package main import ( "fmt" "github.com/go-yaml/yaml" ) var ( cfg Config configData = []byte(`api: telegram_token: "telegramtoken" other_token: "othertoken"`) ) type Config struct { API ConfigAPI `yaml:"api"` } type ConfigAPI struct { TelegramToken string `yaml:"telegram_token"` OtherToken string `yaml:"other_token"` } func (c *Config) parse() { err := yaml.Unmarshal(configData, c) if err != nil { fmt.Println(err.Error()) } } func Init() { cfg.parse() fmt.Printf("%+v\n", cfg) } func main() { Init() } 初始化功能,我就无法获得物业价值。 为什么? 谢谢!

这是有效的:

{API:{TelegramToken:telegramtoken OtherToken:othertoken}}

控制台: package api import ( "fmt" "github.com/go-yaml/yaml" ) var ( cfg Config configData = []byte(`api: telegram_token: "telegramtoken" waves_token: "wavestoken"`) ) type Config struct { API ConfigAPI `yaml:"api"` } type ConfigAPI struct { TelegramToken string `yaml:"telegram_token"` WavesToken string `yaml:"waves_token"` } func (c *Config) parse() { err := yaml.Unmarshal(configData, c) if err != nil { fmt.Println(err.Error()) } } func Init() { cfg.parse() fmt.Printf("%+v\n", cfg) }

不能工作:

package main

api.Init(),请参阅// The daemon that starts the API in background process. package main import ( "fmt" "log" "net/http" "time" "github.com/julienschmidt/httprouter" api "github.com/krypton-code/waves-bot/pkg/api" ) var ( host = "https://api.telegram.org/bot" method = "/getMe" ) // Index - home page. func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { fmt.Fprint(w, "<h1>Server of TelegaBot for Waves Platform is running!</h1>\n") } func main() { api.Init() router := httprouter.New() router.GET("/", Index) s := &http.Server{ Addr: ":8089", Handler: router, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } log.Printf("\nApp listening on port%s. Go to http://localhost:8089/", s.Addr) log.Fatal(s.ListenAndServe()) }

{API:{TelegramToken: WavesToken:}}

控制台: GD123 GD564

1 个答案:

答案 0 :(得分:2)

通过缩进令牌字段来修改YAML以匹配预期的结构:

<?php

class UploadImageResize
{
//If you face any errors, increase values of "post_max_size", "upload_max_filesize" and "memory_limit" as required in php.ini

 //Some Settings
private $ThumbSquareSize        = 100; //Thumbnail will be 200x200
private $BigImageMaxSize        = 2000; //Image Maximum height or width
private $ThumbPrefix            = "thumb_"; //Normal thumb Prefix
private $DestinationDirectory; //Upload Directory ends with / (slash)
private $Quality                = 90;
private $Image;



//ini_set('memory_limit', '-1'); // maximum memory!
public function __construct($tripname)
{
    $this->DestinationDirectory     = 'res/img/Gallery/'.$tripname.'/';
    mkdir($this->DestinationDirectory, 0775);

    foreach ($_FILES as $file) {
// some information about image we need later.
        $ImageName = $file['name'];
        $ImageSize = $file['size'];
        $TempSrc = $file['tmp_name'];
        $ImageType = $file['type'];


        if (is_array($ImageName)) {
            $c = count($ImageName);

            echo '<ul>';

            for ($i = 0; $i < $c; $i++) {
                $processImage = true;
                $RandomNumber = rand(0, 9999999999);  // We need same random name for both files.

                if (!isset($ImageName[$i]) || !is_uploaded_file($TempSrc[$i])) {
                    echo '<div class="error">Error occurred while trying to process <strong>' . $ImageName[$i] . '</strong>, may be file too big!</div>'; //output error
                } else {
                    //Validate file + create image from uploaded file.
                    switch (strtolower($ImageType[$i])) {
                        case 'image/png':
                            $this->setImage(imagecreatefrompng($TempSrc[$i]));
                            break;
                        case 'image/gif':
                            $this->setImage(imagecreatefromgif($TempSrc[$i]));
                            break;
                        case 'image/jpeg':
                        case 'image/pjpeg':

                        $this->setImage(imagecreatefromjpeg($TempSrc[$i]));
                            break;
                        default:
                            $processImage = false; //image format is not supported!
                    }
                    //get Image Size
                    list($CurWidth, $CurHeight) = getimagesize($TempSrc[$i]);

                    //Get file extension from Image name, this will be re-added after random name
                    $ImageExt = substr($ImageName[$i], strrpos($ImageName[$i], '.'));
                    $ImageExt = str_replace('.', '', $ImageExt);

                    //Construct a new image name (with random number added) for our new image.
                    $NewImageName = $RandomNumber . '.' . $ImageExt;

                    //Set the Destination Image path with Random Name
                    $thumb_DestRandImageName = $this->DestinationDirectory . $this->ThumbPrefix . $NewImageName; //Thumb name
                    $DestRandImageName = $this->DestinationDirectory . $NewImageName; //Name for Big Image

                    //Resize image to our Specified Size by calling resizeImage function.
                    if ($processImage && $this->resizeImage($CurWidth, $CurHeight, $this->BigImageMaxSize, $DestRandImageName, $this->getImage(), $this->Quality, $ImageType[$i])) {
                        //Create a square Thumbnail right after, this time we are using cropImage() function
                        if (!$this->cropImage($CurWidth, $CurHeight, $this->ThumbSquareSize, $thumb_DestRandImageName, $this->getImage(), $this->Quality, $ImageType[$i])) {
                            echo 'Error Creating thumbnail';
                        }

                    } else {
                        echo '<div class="error">Error occurred while trying to process <strong>' . $ImageName[$i] . '</strong>! Please check if file is supported</div>'; //output error
                    }

                }

            }
            echo '</ul>';
        }
    }
}

    private function fixExif($filename)
    {

        $exif = exif_read_data($filename);
        if(!empty($exif['Orientation'])) {
            $ort = $exif['Orientation'];

            switch ($ort) {
                case 1: // nothing
                    break;

                case 2: // horizontal flip
                    $this->setImage(imageflip($filename, 1));
                    break;

                case 3: // 180 rotate left
                    $this->setImage(imagerotate($filename, 180, 0));
                    break;

                case 4: // vertical flip
                    $this->setImage(imageflip($filename, 2));
                    break;

                case 5: // vertical flip + 90 rotate right
                    $this->setImage(imageflip($filename, 2));
                    $this->setImage(imagerotate($this->getImage(), -90, 0));
                    break;

                case 6: // 90 rotate right
                    $this->setImage(imagerotate($this->getImage(), -90, 0));
                    break;

                case 7: // horizontal flip + 90 rotate right
                    $this->setImage(imageflip($this->getImage(), 1));
                    $this->setImage(imagerotate($this->getImage(), -90, 0));
                    break;

                case 8:    // 90 rotate left
                    $this->setImage(imagerotate($this->getImage(), 90, 0));
                    break;
            }
        }

    }

// This function will proportionally resize image 
public function resizeImage($CurWidth,$CurHeight,$MaxSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{
    //Check Image size is not 0
    if($CurWidth <= 0 || $CurHeight <= 0) 
    {
        return false;
    }

    //Construct a proportional size of new image
    $ImageScale         = min($MaxSize/$CurWidth, $MaxSize/$CurHeight); 
    $NewWidth           = ceil($ImageScale*$CurWidth);
    $NewHeight          = ceil($ImageScale*$CurHeight);

    if($CurWidth < $NewWidth || $CurHeight < $NewHeight)
    {
        $NewWidth = $CurWidth;
        $NewHeight = $CurHeight;
    }
    $NewCanves  = imagecreatetruecolor($NewWidth, $NewHeight);
    // Resize Image
    if(imagecopyresampled($NewCanves, $SrcImage,0, 0, 0, 0, $NewWidth, $NewHeight, $CurWidth, $CurHeight))
    {
        switch(strtolower($ImageType))
        {
            case 'image/png':
                imagepng($this->getImage(),$DestFolder);
                break;
            case 'image/gif':
                imagegif($this->getImage(),$DestFolder);
                break;          
            case 'image/jpeg':
            case 'image/pjpeg':
                imagejpeg($this->getImage(),$DestFolder,$Quality);
                break;
            default:
                return false;
        }
    if(is_resource($NewCanves)) {
      imagedestroy($NewCanves); 
    }

    return true;
    }

}

//This function corps image to create exact square images, no matter what its original size!
public function cropImage($CurWidth,$CurHeight,$iSize,$DestFolder,$SrcImage,$Quality,$ImageType)
{    
    //Check Image size is not 0
    if($CurWidth <= 0 || $CurHeight <= 0) 
    {
        return false;
    }

    //abeautifulsite.net has excellent article about "Cropping an Image to Make Square"
    //http://www.abeautifulsite.net/blog/2009/08/cropping-an-image-to-make-square-thumbnails-in-php/
    if($CurWidth>$CurHeight)
    {
        $y_offset = 0;
        $x_offset = ($CurWidth - $CurHeight) / 2;
        $square_size    = $CurWidth - ($x_offset * 2);
    }else{
        $x_offset = 0;
        $y_offset = ($CurHeight - $CurWidth) / 2;
        $square_size = $CurHeight - ($y_offset * 2);
    }

    $NewCanves  = imagecreatetruecolor($iSize, $iSize);
    if(imagecopyresampled($NewCanves, $SrcImage,0, 0, $x_offset, $y_offset, $iSize, $iSize, $square_size, $square_size))
    {
        switch(strtolower($ImageType))
        {
            case 'image/png':
                imagepng($NewCanves,$DestFolder);
                break;
            case 'image/gif':
                imagegif($NewCanves,$DestFolder);
                break;          
            case 'image/jpeg':
            case 'image/pjpeg':
                imagejpeg($NewCanves,$DestFolder,$Quality);
                break;
            default:
                return false;
        }
    if(is_resource($NewCanves)) { 
      imagedestroy($NewCanves); 
    } 
    return true;

    }

}
    public function getImage()
    {
        return $this->Image;
    }

    public function setImage($Image)
    {
        $this->Image = $Image;
    }
}