How to call Go function of test1 in test2 - go

go file in as below
package goClientLib
import (
....
)
//The following function will read Command Line Inputs and will return 3 strings
func readInput() (string, string, string){
var (clientRequest, clientId, clientPassword string)
argsLen := len(os.Args)
fmt.Println("Arg Length:",argsLen)
if len(os.Args) != 4 {
fmt.Fprintf(os.Stderr, "Usage: %s URL\n", os.Args[0])
os.Exit(1)
} else {
clientRequest = strings.Join(os.Args[1:2],"")
clientId = strings.Join(os.Args[2:3],"")
clientPassword = strings.Join(os.Args[3:4],"")
}
return clientRequest, clientId, clientPassword
}
Now I am trying to use it in Test2.go file as shown below:
package main
import (
"os"
"fmt"
"net/http"
"io"
"log"
"goClientLib"
)
func main() {
clientRequest, clientId, clientPassword := goClientLib.readInput()
host := goClientLib.generateRequest(clientRequest)
fmt.Println("clientRequest:",clientRequest)
fmt.Println("clientId:",clientId)
fmt.Println("clientPassword:",clientPassword)
fmt.Println("host:",host)
response, err := http.Get(host)
if err != nil {
log.Fatal(err)
} else {
defer response.Body.Close()
_, err := io.Copy(os.Stdout, response.Body)
if err != nil {
log.Fatal(err)
}
}
}
I am using Following File Structure
src/test2.go
src/goClientLib/test1.go
But This code Give me following error while running
# command-line-arguments
src\goClientMain.go:15: cannot refer to unexported name goClientLib.readInput
src\goClientMain.go:16: cannot refer to unexported name goClientLib.generateRequest
src\goClientMain.go:16: undefined: goClientLib.generateRequest

As Volker commented, in order to access functions from another package, 1st letter of function name must be capital. In your case, change readInput() to ReadInput() and generateRequest() to GenerateRequest() and make sure GenerateRequest() function is defined in goClientLib package. Chack this for more information.

Related

Extract filename from io.ReadCloser

I need to get the filename of certain file(s) that receives backend from the frontend. Backend (implemented in Go) will receive the file as io.ReadCloser. Is there way I could extract it from the io.ReadCloser?
Backend (implemented in Go) will receive the file as io.ReadCloser. Is there way I could extract it from the io.ReadCloser?
No.
Take a look at which methods an io.ReadCloser provides by running go doc io.ReadCloser and note that there isn't a method which will provide a name. So unless you know nothing more that that it is an io.ReadCloser you simply cannot do it.
package main
import (
"errors"
"fmt"
"io"
"os"
)
func fatalln(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// hasName interface is an interface that expects types
// that implements it to have "Name() string" method.
type hasName interface {
Name() string
}
func open(name string) (io.ReadCloser, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
// f implements io.ReadCloser interface as *os.File
// has Read and Close methods.
return f, nil
}
func main() {
// rc is of the type io.ReadCloser
rc, err := open("example.txt")
if err != nil {
fatalln(err)
}
defer rc.Close()
// Type assetion to check rc's underlying type has
// a method "Name() string".
f, ok := rc.(hasName)
if !ok {
fatalln(errors.New("type assertion failed"))
}
// Yay, type assertion succeeded. Print the name!
fmt.Println("Name:", f.Name())
}
The io.ReadCloser here is a reader for runtime reader which reads file from network as the frontend sends it to backend. You'll have to work on request itself to get that file name.
This is an assumption but in most such cases for file upload, the request is a multipart request. If you have the same situation, you can read the headers, typically Content-Disposition to identify the file type. Go native http.Request has ability to parse the details. You can try this :
formFile, handler, err := r.FormFile("file") // read file from network with key "file"
defer formFile.Close()
fileName := handler.Filename // Get file name
By defining an interface which embeds io.Reader you can require a Name() method up front:
package main
import (
"fmt"
"io"
"log"
"os"
)
type NamedReadCloser interface {
io.ReadCloser
Name() string
}
func doThings(f NamedReadCloser) error {
defer f.Close()
b, err := io.ReadAll(f)
if err != nil {
return err
}
fmt.Printf("Name: %s, Content: %s\n", f.Name(), b)
return nil
}
func main() {
f, err := os.Open("/etc/hosts")
if err != nil {
log.Fatal("Cannot open file: ", err)
}
err = doThings(f)
if err != nil {
log.Fatal("Error doing things: ", err)
}
}
This will only work if what is passed in has a name method, like an *os.File. If it does not, then what you are trying to do is not possible.
You'll have to cast it to a type with a Name method:
package main
import (
"io"
"os"
)
func open(name string) (io.ReadCloser, error) {
return os.Open(name)
}
func main() {
c, e := open("file.txt")
if e != nil {
panic(e)
}
defer c.Close()
f := c.(*os.File)
println(f.Name())
}

How to create a hidden file in Windows/Mac/Linux?

I build an console application, need create some hidden files.
As well I know filename start with dot will hidden in Linux and mac, but windows?
Set file attributes?
Is there a way to create hidden files and directories in both Windows / Linux / Mac?
Windows:
SetFileAttributesW function
Sets the attributes for a file or directory.
FILE_ATTRIBUTE_HIDDEN 2 (0x2)
The file or directory is hidden. It is not included in an ordinary
directory listing.
Go:
Package syscall
func SetFileAttributes
func SetFileAttributes(name *uint16, attrs uint32) (err error)
Convert from a Go UTF-8 encoded string (string) to a Windows UTF-16 encoded string pointer (*uint16).
Package syscall
func UTF16PtrFromString
func UTF16PtrFromString(s string) (*uint16, error)
UTF16PtrFromString returns pointer to the UTF-16 encoding of the UTF-8
string s, with a terminating NUL added. If s contains a NUL byte at
any location, it returns (nil, EINVAL).
Use OS Build Contraints.
For example,
hide/attrib.go:
package main
import (
"fmt"
"io/ioutil"
"os"
)
func main() {
filename := `test.hidden.file`
os.Remove(filename)
os.Remove("." + filename)
err := ioutil.WriteFile(filename, []byte(filename), 0666)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
err = HideFile(filename)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Println("hidden:", filename)
}
hide/hide.go:
// +build !windows
package main
import (
"os"
"path/filepath"
"strings"
)
func HideFile(filename string) error {
if !strings.HasPrefix(filepath.Base(filename), ".") {
err := os.Rename(filename, "."+filename)
if err != nil {
return err
}
}
return nil
}
hide/hide_windows.go:
// +build windows
package main
import (
"syscall"
)
func HideFile(filename string) error {
filenameW, err := syscall.UTF16PtrFromString(filename)
if err != nil {
return err
}
err = syscall.SetFileAttributes(filenameW, syscall.FILE_ATTRIBUTE_HIDDEN)
if err != nil {
return err
}
return nil
}
Output (Linux):
$ tree hide
hide
├── attrib.go
├── hide.go
└── hide_windows.go
$
$ go build && ./hide
hidden: test.hidden.file
$ ls -a .test.hidden.file
.test.hidden.file
$
Output (Windows):
>go build && hide
hidden: test.hidden.file
>attrib test.hidden.file
A H \test.hidden.file
>
I made a cross-platform module for this (it's available here: higgs).
You can hide or unhide file or directory simply by calling Hide or Unhide functions.
Sample code:
package main
import (
"fmt"
"github.com/dastoori/higgs"
)
func main() {
err := higgs.Hide("foo.txt")
if err != nil {
fmt.Println(err)
}
}
Make a file like this:
//go:generate mkwinsyscall -output zhide.go hide.go
//sys setFileAttributes(name string, attr int) (err error) = kernel32.SetFileAttributesW
package main
const (
file_attribute_hidden = 2
file_attribute_normal = 128
)
func main() {
setFileAttributes("file.txt", file_attribute_hidden)
}
Then build:
go mod init hide
go mod tidy
go generate
go build
import (
_ "golang.org/x/sys/windows"
"os"
"runtime"
"syscall"
)
func HideFile(filename string) (status bool, err error) {
if runtime.GOOS == "windows" {
filenameW, err := syscall.UTF16PtrFromString(filename)
if err != nil {
return false, err
}
err = syscall.SetFileAttributes(filenameW, syscall.FILE_ATTRIBUTE_HIDDEN)
if err != nil {
return false, err
}
} else {
if filename[0:1] != "." {
err = os.Rename(filename, "." + filename)
if err != nil {
return false, err
}
}
}
return true, nil
}
That's my code. But can't build in Linux。
Errors:
src/util/hidden.go:12:21: undefined: syscall.UTF16PtrFromString
src/util/hidden.go:17:9: undefined: syscall.SetFileAttributes
src/util/hidden.go:17:46: undefined: syscall.FILE_ATTRIBUTE_HIDDEN

GORM DB Connection on other package

I start learning Go, reading about pointers, and want to split my database connection , and handler function for API. Already tried myself, by following this solution , but when i trying to read data, i am having this error
[2018-06-26 21:59:45] sql: database is closed
this is my source code.
db.go
package db
import (
"fmt"
"github.com/jinzhu/gorm"
"github.com/joho/godotenv"
"os"
)
var Db *gorm.DB
func Open() error {
var err error
_ = godotenv.Load(".env")
dbType := os.Getenv("DB_TYPE")
dbConnString := os.Getenv("DB_CONN_STRING")
Db, err = gorm.Open(dbType, dbConnString)
if err != nil {
fmt.Println(err)
}
Db.LogMode(true)
defer Db.Close()
return err
}
func Close() error {
return Db.Close()
}
person.go
package model
import (
"github.com/jinzhu/gorm"
"github.com/gin-gonic/gin"
"fmt"
"namastra/gin/result"
"namastra/gin/db"
)
type Person struct {
gorm.Model
FirstName string `json:”firstname”`
LastName string `json:”lastname”`
}
/*var db *gorm.DB
var err error*/
func GetPeople(c *gin.Context) {
var people []result.Person
if err := db.Db.Select("ID,first_name,last_name").Find(&people).Error; err != nil {
c.AbortWithStatus(404)
fmt.Println(err)
} else {
c.JSON(200, people)
}
}
main.go
package main
import (
"log"
"namastra/gin/handler"
"namastra/gin/model"
"net/http"
"time"
"github.com/adam-hanna/jwt-auth/jwt"
"github.com/gin-gonic/gin"
_ "github.com/jinzhu/gorm/dialects/postgres"
"namastra/gin/db"
)
func main() {
if err := db.Open(); err != nil {
// handle error
panic(err)
}
defer db.Close()
router := gin.Default()
router.Use(gin.Recovery())
private := router.Group("/auth")
....(ommited)
router.GET("/", gin.WrapH(regularHandler))
router.GET("/people/", model.GetPeople)
router.Run("127.0.0.1:3000")
}
Sorry for my bad english, any kind of help is appreciated.
thank you.
edit1: case closed.
solution is by removing
defer Db.Close()
from db.go.
edi2: update some knowledge i learn by working in go project
As start learning GO, usually we put everything on single main.go file, and we think to split the code to multiple files.
That is the time Dependency Injection comes to play.
we can create something like this Env to store the handler.
type Env struct {
db *sql.DB
logger *log.Logger
templates *template.Template
}
and create something like this in models/db.go
package models
import (
"database/sql"
_ "github.com/lib/pq"
)
func NewDB(dataSourceName string) (*sql.DB, error) {
db, err := sql.Open("postgres", dataSourceName)
if err != nil {
return nil, err
}
if err = db.Ping(); err != nil {
return nil, err
}
return db, nil
}
main.go files
package main
import (
"namastra/gin/models"
"database/sql"
"fmt"
"log"
"net/http"
)
type Env struct {
db *sql.DB
}
func main() {
db, err := models.NewDB("postgres://user:pass#localhost/bookstore")
if err != nil {
log.Panic(err)
}
env := &Env{db: db}
http.HandleFunc("/peoples", env.peoplesIndex)
http.ListenAndServe(":3000", nil)
}
func (env *Env) peoplesIndex(w http.ResponseWriter, r *http.Request) {
# ...
}
and in models/people.go
package models
import "database/sql"
type Book struct {
Isbn string
Title string
Author string
Price float32
}
func AllPeoples(db *sql.DB) ([]*People, error) {
rows, err := db.Query("SELECT * FROM peoples")
if err != nil {
return nil, err
}
defer rows.Close()
# ... ommited for simplicity
}
you can read the full code & explanation in Alex Edwards post

Mocking functions called from within another in golang

I am trying to stub os.Stat and ioutil.ReadFile(path) as used the code below or if you like here on go playground [1]
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func AssignFileValueFrom(path string, val *string) {
var (
tempValue []byte
err error
)
if _, err = os.Stat(path); err == nil {
if err != nil {
fmt.Println("There was a os stat error:", err)
}
tempValue, err = ioutil.ReadFile(path)
if err != nil {
fmt.Println("There was an io read error:", err)
}
*val = strings.TrimSpace(string(tempValue))
}
}
I have used testify and tried following the example here [2]
package main
import (
"testing"
"github.com/stretchr/testify/mock"
)
type osMock struct {
mock.Mock
}
func (o osMock) Stat(path string) (interface{}, error) {
return nil, nil
}
func TestAssignFileValueFrom(t *testing.T) {
var test string
osm := new(osMock)
osm.On(`Stat`, `./.test`).Return([]byte(`1`), nil)
AssignFileValueFrom(`./.test`, &test)
// assert.Equal(t, `1`, test)
osm.AssertExpectations(t)
}
What am I not doing correctly??
[1] https://play.golang.org/p/xcbdMkMwoBN
[2] https://github.com/stretchr/testify#mock-package
Your code with osMock doesn't any how influence execution of AssignFileValueFrom function. There is a direct call of os.Stat and it won't be substituted just because you have declared osMock somewhere.
To do actual testing you should use interfaces and dependency injection to be able to test your code.
First of all os.Stat call must be substituted with call to your struct that implements an interface with same method defined. And you need to create at least 2 implementations of this interface: 1 - is actual working code to use, 2 - mock as your osMock struct to use in test. And you need to inject it or pass it to AssignFileValueFrom and then use to call Stat method on it.
Thanks, guys for your inputs I have rewritten my tests as follows..
package main
import (
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/assert"
)
var (
err error
testFile *os.File
test string
)
const (
TestPrefix = `test_file_prefix`
FileContent = `1234`
)
func init() {
testFile, err = ioutil.TempFile(os.TempDir(), TestPrefix)
if err != nil {
panic(err)
}
err = ioutil.WriteFile(testFile.Name(), []byte(FileContent), 0644)
if err != nil {
panic(err)
}
}
func TestAssignFileValueFrom(t *testing.T) {
AssignFileValueFrom(testFile.Name(), &test)
assert.Equal(t, test, FileContent)
}

How to read a YAML file

I have an issue with reading a YAML file. I think it's something in the file structure but I can't figure out what.
YAML file:
conf:
hits:5
time:5000000
code:
type conf struct {
hits int64 `yaml:"hits"`
time int64 `yaml:"time"`
}
func (c *conf) getConf() *conf {
yamlFile, err := ioutil.ReadFile("conf.yaml")
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
return c
}
your yaml file must be
hits: 5
time: 5000000
your code should look like this:
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
)
type conf struct {
Hits int64 `yaml:"hits"`
Time int64 `yaml:"time"`
}
func (c *conf) getConf() *conf {
yamlFile, err := ioutil.ReadFile("conf.yaml")
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}
err = yaml.Unmarshal(yamlFile, c)
if err != nil {
log.Fatalf("Unmarshal: %v", err)
}
return c
}
func main() {
var c conf
c.getConf()
fmt.Println(c)
}
the main error was capital letter for your struct.
Example
Using an upgraded version 3 of yaml package.
An example conf.yaml file:
conf:
hits: 5
time: 5000000
camelCase: sometext
The main.go file:
package main
import (
"fmt"
"io/ioutil"
"log"
"gopkg.in/yaml.v3"
)
type myData struct {
Conf struct {
Hits int64
Time int64
CamelCase string `yaml:"camelCase"`
}
}
func readConf(filename string) (*myData, error) {
buf, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
c := &myData{}
err = yaml.Unmarshal(buf, c)
if err != nil {
return nil, fmt.Errorf("in file %q: %w", filename, err)
}
return c, err
}
func main() {
c, err := readConf("conf.yaml")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%#v", c)
}
Running instructions (in case it's the first time you step out of stdlib):
cat conf.yaml
go mod init example.com/whatever
go get gopkg.in/yaml.v3
cat go.sum
go run .
About The yaml:"field"
The tags (like yaml:"field") are optional for all-lowercase yaml key identifiers. For demonstration the above example parses an extra camel case identifier which does require such a tag.
Corner Case: JSON+YAML
Confusingly, the useful lowercasing behavior of yaml package is not seen in the standard json package. Do you have a data structure which is sometimes encoded to JSON and sometimes to YAML? If so, consider specifying both JSON tags and YAML tags on literally every field. Verbose, but reduces mistakes. Example below.
type myData struct {
Conf conf `yaml:"conf" json:"conf"`
}
type conf struct {
Hits int64 `yaml:"hits" json:"hits"`
Time int64 `yaml:"time" json:"time"`
CamelCase string `yaml:"camelCase" json:"camelCase"`
}
package main
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
)
type someConf struct {
AccessKeyID string `yaml:"access_key_id"`
//...
}
func getConf(file string, cnf interface{}) error {
yamlFile, err := ioutil.ReadFile(file)
if err == nil {
err = yaml.Unmarshal(yamlFile, cnf)
}
return err
}
func main() {
cfg := &awsConf{}
if err := getConf("conf.yml", cfg); err != nil {
log.Panicln(err)
}
}
shortest getConf ever

Resources