Go import local package - go

I have a main.go
package main
import (
"context"
"fmt"
"log"
model "model"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
func handler(...){
}
I try to import model which is in the directory model and the file is called model.go
It just contains:
package model
type xxx struct {
xxx
}
I try to import this in the main but I have the error:
build: cannot load model: cannot find module providing package model

If your module model is not local then you can use Tonys answer and it will work fine but if you are using this module locally then you will need to add the paths in your go.mod file.
So for example, Local module model contains only model.go which has the following content
package model
type Example struct {
Name string
}
func (e *Example) Foo() string {
return e.Name
}
For this local module must have have to init the module with the command go mod init model and the content of the ./model/go.mod will be
module model
go 1.13
In the main module in which you are importing this module you need to add the following line
require model v1.0.0
replace model v1.0.0 => {Absolute or relative path to the model module}
So, your main testing module's go.mod file will look like this
module main
require model v1.0.0
replace model v1.0.0 => ./model
go 1.13
By setting this up you can use this module in this test module with just import "model"
Hence when testing the module with the main method
package main
import (
model "model"
)
func main() {
example := model.Example{
Name: "Hello World",
}
println(example.Foo())
}
The output will be
Hello World

If your go.mod looks like this:
module github.com/meakesbia/myproject
go 1.14
then you need to import the module package using the full module reference:
import "github.com/meakesbia/myproject/model"
If it's an entirely local project then replace github.com/meakesbia with the model name from go.mod e.g.:
module meakesbia/myproject
go 1.14
import "meakesbia/myproject/model"
You don't need to add a replace directive to the go.mod file unless you're making local changes to a module that is imported from e.g. github.

Related

Import and use NPM package (Sluggo) in Astro

I want to use this NPM package, Sluggo (https://www.npmjs.com/package/sluggo), in Astro, in the frontmatter of an Astro component.
After installation, I import it following Astro docs:
import { sluggo } from 'sluggo';
When I try to use it following Sluggo docs:
var sluggedString = sluggo('# monkey\'s are elab؉؉orate fools##');
I get this error: vite_ssr_import_1.sluggo is not a function
I can understand that "sluggo" is not a function, but how can I access the function inside the package?
It looks like sluggo is not a named export, so you need to do:
import sluggo from 'sluggo';

Ember 2.16.x: Accessing Ember.Handlebars.Utils.escapeExpression with new import syntax

How does one access the Ember.Handlebars.Utils.escapeExpression function using the new import syntax in Ember 2.16.x and above?
The following code snippet comes from the Writing Helpers section of the Ember docs. (FYI, there are a couple of unrelated errors in the original, which I have cleaned up in the code below.)
import { helper } from "#ember/component/helper";
import Handlebars from "handlebars";
import { htmlSafe } from "#ember/string";
export function makeBold(param /*, ...rest*/ ) {
let value = Handlebars.Utils.escapeExpression(param);
return htmlSafe(`<b>${value}</b>`);
}
export default helper(makeBold);
If I use the code above, I get the following error:
Could not find module 'handlebars' imported from 'ember-app/helpers/make-bold'
As of right now the Handlebars.Utils.escapeExpression function is not yet exported by the New Module Imports (aka. RFC 176). You should keep using it from the Ember import for now:
import Ember from 'ember';
Ember.Handlebars.Utils.escapeExpression(...)
An open GitHub issue for this exists at https://github.com/ember-cli/ember-rfc176-data/issues/12
The guides page that you linked appears to be mistaken and we need to fix that particular snippet. Sorry about that!

Adding method to a golang struct in a different file

How would one go about adding a method to a struct that is a different file?
This is what I've tried so far but it doesn't seem to be working.
// ./src
package routes
type App struct{
}
func (a *App) initializeRoutes() {
a.Subrouter.HandleFunc("/products", a.getSomething).Methods("GET")
}
// ./src/controller
package routes
func (a *App) getSomething(w http.ResponseWriter, r *http.Request){
...
}
They are in the same package.
They are not in the same package. A Go package has both a name and a path. They're both named routes but they have different paths. The actual packages are routes and controller/routes. The result is subdirectories are different packages.
See Package Names on the Go Blog for more information.
Since they're in different packages, they can only access each other's public members and exported methods. You can't monkey patch someone else's package or interface in Go. This is by design to keep all the functionality of a package in one place, no action-at-a-distance.
You have options. You could put all methods of routes into a single package. If they all belong together, there's no need to split it up into multiple files.
If they don't really belong together, you can write a new struct with routes embedded into it and define new methods on that. Then you can access either the wrapper struct to get your added methods, or its embedded struct to get routes' methods. See this answer for an example.
But really I think you need to think about how your code is arranged. The App probably shouldn't be defined by the routes package, they should be separate. Instead, Go prefers a has-a relationship. App would contain an instance of routes.Route.
You'd rearrange your code tree like so:
app/
app.go
app_test.go
routes/
routes.go
routes_test.go
Note that rather than having it all in src/ it's now contained in its own project directory. app.go would look something like this.
// src/app/app.go
package app
import(
"app/routes"
"fmt"
"net/http"
)
type App struct {
routes routes.Route
}
func (a *App) initializeRoutes() {
a.routes.AddRoute("/products", a.getSomething)
}
func (a *App) getSomething(w http.ResponseWriter, r *http.Request) {
fmt.Println("Something!")
}
Notice how we're delegating responsibility to adding a route to a.routes rather than having App do it itself. This avoids the desire to smash all functionality into one ginormous package. routes.Route would be defined in app/routes/routes.go.
// src/app/routes/routes.go
package routes
import "net/http"
// A type specifying the interface for route handlers.
type RouteHandler func(w http.ResponseWriter, r *http.Request)
type Route struct {
handlers map[string]RouteHandler
}
func (r *Route) AddRoute(path string, handler RouteHandler) {
r.handlers[path] = handler
}
Now all routes has to worry about is handling routes. It doesn't know anything about your specific application logic.
I was trying to get my http.res & http.req functions in a controllers file.
Now that we've rearranged the file structure, you can do that. You can, if you like, define app/controllers.go to organize your code.
// src/app/controllers.go
package app
import(
"fmt"
"net/http"
)
func (a *App) getSomething(w http.ResponseWriter, r *http.Request) {
fmt.Println("Something!")
}
app/app.go and app/controllers.go are in the same package. They have the same path and the same name. So app/controllers.go can add methods to App.
You have an error because your files belong to different packages. Everything related to one struct must be in the same package.
It is possible to declare struct and its methods in different files, but they must belong same package (be in same folder).

Revel Framework - Go Lang - Not able to find controller

I am using Revel framework for golang. I have a subdirectory in my controllers folder like below
controllers
new
app2.go
app1.go
Contents of app1.go
package controllers
import (
"github.com/revel/revel"
)
type APP1 struct {
*revel.Controller
}
func (c APP1) Show() revel.Result {
}
Contents of app2.go
import (
"github.com/revel/revel"
)
type APP2 struct {
*revel.Controller
}
func (c APP2) Show() revel.Result {
}
My routes file is like this
GET /v1/show APP1.show
GET /v2/show APP2.show
When I call /v2/show it gives me error failed to find controller APP2 while v1/show is working absolutely working fine. Can anybody tell me how to fix it.
Configure the route for APP2.show like this GET /new/v2/show
Add the below at the top of app2.go
package controllers
I tried the same using a similar example and it worked for me. I would suggest not to use full capitalized controller names; use App2 instead of APP2.
I was a little confused to find out that the methods referenced from the router need to be Pascal Cased to be recognized (e.g. like above func (c APP2) DescribeService() revel.Result). This might be obvious to seasoned Revel or Go developers but really wasn't obvious to me.

How do I use a package language file?

I'm trying to use a Laravel 4 package language file, but I don't know how to do it.
I created a package with php artisan workbench vendor/package --resources. I then create file workbench/vendor/package/src/lang/en/routes.php.
In that routes file a I have this:
<?php
return [
'foo' => 'bar'
];
Now how do I access that? I tried with Lang::get('routes.foo') and Lang::get('vendor/package::routes.foo') but both fails and just gives me the parameter itself I entered. I'm calling it in my service providers boot method.
Same like you call view and config:
// for lang
Lang::get('package::routes.foo')
// or with shortcut func
trans('package::routes.foo')
// for view
View::make('package::view.name');
// for config
Config::get('package::group.option');
What you need to do is to remove vendor/ but leave package.
You can see more at the Laravel documentation on: package conventions.
====
UPDATE
in laravel 5 you can call view and config like this :
// for view (shorthand)
view('path_to_view', array('data' => 'somedata'));
// for config
config('config.name', 'default');

Resources