Search code examples
phpgowav

Read *.wav file in golang


I've used file_get_contents for reading a WAV file in PHP and I want to use package github.com/mjibson/go-dsp/wav for same task in Go. But there is not any simple example about this package. I am new to Go and do not understand it. Is there anyone guiding me or suggest another way?

Code in PHP:

$wsdl = 'http://myApi.asmx?WSDL';
$client = new SoapClient($wsdl));
$data = file_get_contents(public_path() . "/forTest/record.wav");
$param = array(
  'userName' => '***',
  'password' => '***',
  'file' => $data);

$res = $client->UploadMessage($param);

Solution

  • It looks like you do not need to use this package github.com/mjibson/go-dsp/wav. file_get_contents function is the preferred way to read the contents of a file into a string.

    In Go, you can do something like this:

    package main
    
    import (
        "fmt"
        "io/ioutil"
    )
    
    func public_path() string {
        return "/public/path/"
    }
    
    func main() {
        dat, err := ioutil.ReadFile(public_path() + "/forTest/record.wav")
        if err != nil {
            fmt.Println(err)
        }
        fmt.Print(string(dat))
    }
    

    https://play.golang.org/p/l9R0940iK50