How to install Gin with Golang - linux

I'm a newbie on Golang, and I'm trying to use Gin to develop a web server on Ubuntu 16.04.
After executing go get -u github.com/gin-gonic/gin, many folders appear at ~/go/pkg/mod/github.com/.
Then I try to make an example:
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
However, go run example.go made the error:
example.go:3:8: cannot find package "github.com/gin-gonic/gin" in any of:
/usr/local/go/src/github.com/gin-gonic/gin (from $GOROOT)
/home/zyh/go/src/github.com/gin-gonic/gin (from $GOPATH)
In my system, $GOROOT is /usr/local/go/ and $GOPATH is ~/go/.
How could I solve this problem?

For Go version 1.11 or newer, You should use Go Modules.
If you are just starting with Go, you should start with the newer version. I think you are using a Go version that supports go modules already because the modules you are trying to get are downloading to ~/go/pkg/mod/ directory.
To initialize a project with go module, run:
go mod init your-project-name
This will create a go.mod file in your project directory.
Add missing and/or remove unused modules:
go mod tidy
This will fill up the go.mod file with appropriate modules and create a go.sum in your project directory. The go.sum contains expected cryptographic hashes of each module version.
After that, the go run example.go command should run the program without any issues.
You can even vendor the modules in your project directory:
go mod vendor
This will bring all the vendors to your projects /vendor directory so that you don't need to get the modules again if working from another machine on this project.

I realized that after adding a package called gopls, my IDE is working perfectly.
Install gopls using snap: sudo snap install gopls --classic

From the error, you can see that GOPATH is your '/home/zyh/go' not your ~/go.
and you can run shell go env to confirm where is your GOPATH? then modify it.

Related

Domino10 appDevPack: "Error: Cannot find module '#domino/domino-db'"

Just installed the latest Domino 10.0.1 Server on my linux machine and also installed and configured the latest proton package. As far as I can tell it's all running fine.
Next I plan to try my first Node-RED flow using the new Domino10 nodes. So I installed the 'node-red-contrib-dominodb' palette.
Finally tried my first very simple flow trying to query node-demo.nsf as it's described here. From what I read there I assumed that it's sufficient to install the palette, but that obviously is not the case:
as soon as I hit 'Deploy' I receive this error:
Error: Cannot find module '#domino/domino-db'
So I thought that I maybe still have to do a global install in node.js using
npm install -g <package-path>/domino-domino-db-1.1.0.tgz
This indeed created a local #domino/domino-db module inside my node.js npm\node_modules folder. But obviously my node-red environment doesn't know about it.
Question is: how do I register / install that npm package for my local node-red environment?
IBM's instructions (https://flows.nodered.org/node/node-red-contrib-dominodb#Installation)
Say to go view this guide(https://github.com/stefanopog/node-red-contrib-dominodb/blob/master/docs/Using%20the%20new%20Domino%20V10%20NodeRED%20nodes%202.pdf) for installing the domino-db module.
The link is broken, here's an old copy: https://github.com/stefanopog/node-red-contrib-dominodb/blob/a723ef88498c5bfa243abd956a7cc697f0a42610/docs/Using%20the%20new%20Domino%20V10%20NodeRED%20nodes%202.pdf
I believe the section you want is called "Import the tarball". The steps before that require you to unpack and then re-pack the module... which is unnecessary. Just use the tgz that was in the AppDev Pack to begin with.

Flow+Webstorm "Cannot resolve module"

I have an existing project where we integrated Flow's type system into the react side. The project is electron-based so, by definition, a mono-repo. We ran in to all kinds of issues getting flow to recognize import statements.
node_modules imports would fail:
import _ from 'lodash'; // Flow: Cannot resolve module lodash
And more importantly, we wanted absolute pathing relative to our project:
import {MyComponent} from 'src/component/myComponent';
// Flow: Cannot resolve module src...
Finding a solution on this took a bit of digging, and the documentation is a little lacking in some areas, so I want to throw a compiled list of what actually worked out there.
TL;DR;
Get flow set up on webstorm so it is giving you module errors
Set up flow globally, and point webstorm's js settings to use Flow and point it at the global copy of flow-bin (not even the exe, just the dir)
add the following options to .flowconfig:
[options]
module.name_mapper='^src\/\(.*\)$' -> '<PROJECT_ROOT>/src/\1'
module.system=haste
Full version
A few basic steps have to be done to get flow to work in webstorm at all:
Install flow-bin globally
Several sources made the claim that flow-bin runs better globally
Install flow-bin globally
yarn global add flow-bin
or
npm i -g flow-bin
Double check that it gave you a current version of flow-bin, this
refused to work on 0.75.0 or earlier
Set up Webstorm's flow executable
On Webstorm: File > Settings > Languages & Frameworks > Javascript
Choose Flow as the JavaScript language version
Find where your package manager (yarn or npm) stores global files
On Windows+yarn this is C:/Users/[your username]/AppData/Local/Yarn/Cache/v1
On Windows+NPM this is C:/Users/fish/AppData/Roaming/npm/node_modules
That makes my flow path:
C:/Users/[your username]/AppData/Local/Yarn/Cache/v1/npm-flow-bin-[whatever]/
On Webstorm's JavaScript settings, Target the Flow package or executable to the global flow path we just found
Apply, ok
Setting up Flow's .flowconfig
.flowconfig setup side notes
I have a root git project with 2 parts, react, and electron. Flow does things based on where you put the .flowconfig file.
If it includes "all=true", remove that line and go add // #Flow to your files you want flow to check (otherwise it will start indexing all of node_modules
Reproducing my problem
Put .flowconfig in your react directory
Enjoy all the "Flow: Cannot import module" squiggly lines of doom
Solution to the module problem
This is my current .flowconfig
[ignore]
.*/build/.*
[include]
[libs]
[lints]
[options]
module.name_mapper='^src\/\(.*\)$' -> '<PROJECT_ROOT>/src/\1'
module.system=haste
[strict]
Why does this work?
Tells the name mapper to resolve modules that begin with src/ to the src/ directory so your absolute paths to your project's files work:
module.name_mapper='^src\/\(.*\)$' -> '<PROJECT_ROOT>/src/\1'
Tells flow to use the "haste" module system:
module.system=haste
The haste module system step is important because otherwise it doesn't know that by 'lodash' you mean './node_modules/lodash'. Telling it to use haste means it will properly find your import statements. More info on haste available here

Error after packaging the app with electron-packager

I'm new to Electron, and I really love it so far, but I'm unable to package any of mine apps, at first I thought that it's maybe something related to my code, then I download "https://github.com/atom/electron-quick-start" run npm install and then I run "electron-packager . FooBar --platform=darwin --arch=x64 --version=0.28.2" it build the app but when I try to open it I get
so I didn't touch any code from the example, just wanted to build it and I got an error, what am I doing wrong? Thanks!
The versions of electron are moving very very fast.
And some times, they don't respect the "old" ways to do things (for example, declaring the app).
I advise you to not use the 0.28.2 version of electron but the most recent one.
It is very likely that the version of electron-prebuilt you are using to develop is much much much more recent than the 0.28.2 version. So, you are developing with something much newer, and then you are building with 0.28.2. This would cause the exact error that you are seeing, as older versions may not have had the electron module, which your code explicitly is importing. So... that is my suggestion. Change the version in your electron-packager command from 0.28.2 to something like 0.36.0. See if that works. Or better yet, use the same version as electron-prebuilt in your package.json.
This could be a combination of factors.
First, as others stated, the version of electron that you have might be newer than the one referenced in your build command. Locate the 'electron_prebuilt' folder inside your 'node_modules' folder, and examine the package.json file and make sure the version # is the same as what you are declaring in your build command.
If they are the same, then the issue might be that you have another version of electron on your computer that node is trying to use. If you installed electron via the -g option (global), check your home folder to see if there is another different version of electron. If you find one, either delete it or rename the 'electron_prebuilt' folder you find to something else. Try your build command again, and it should work now that you've eliminated the other versions of electron_prebuilt on your computer that node was referencing.
What worked for me was to move the "electron" module from "dev-dependencies" to "dependencies" in package.json. Try this and see if it works.

Downloading source files cache modernizr.load.1.5.4.js Grunt Build Stall

I have been using Yeoman to start building with web apps and such for maybe the last month. And I have been running into some issues and have been able to resolve most of them. However, now I am stuck.
I'm running MAC OS 10.6.8
I have reinstalled Yeoman and Node fixed my paths for (what I know) as global.
Running grunt in my app.
Forces me to run as sudo. (I think this is because this OS has password protected permissions for apps and programs to install/modify files.
** If I run grunt -f
Warning: Unable to write "dist/scripts/vendor/modernizr.js" file (Error code: EACCES). This error happens with most of the main tasks in grunt.
Running sudo grunt
grunt runs through the tasks just fine until the real issue I cannot locate an answer.
Running "modernizr" task
Enabled Extras
shiv
load
cssclasses
Looking for Modernizr references
in dist/styles/main.css
svg
input
Downloading source files
cache modernizr.load.1.5.4.js
And grunt will not finish this download of source files. When I ran grunt using force it would complete the download. I am looking for a solution for the 2nd list item. Any assistance would be helpful.
Found out that there some glitch in the npm grunt-modernizr.
I used this code:
npm remove grunt-moderizr && npm install --save-dev grunt-modernizr
found here: https://github.com/Modernizr/grunt-modernizr/issues/48

Error code: 800A1391 Source: Microsoft JScript runtime error Running Grunt - Module is undefined

New Grunt user here who is using a lot of new tools (npm nodejs) today.
I've got Grunt "installed" and have been able to create a grunt.js file using the init task as described here: http://net.tutsplus.com/tutorials/javascript-ajax/meeting-grunt-the-build-tool-for-javascript/ and here: https://github.com/cowboy/grunt/blob/master/docs/getting_started.md. But whenever I run the "grunt" command I get an error:
Windows Script Host
Script: c:\users\[]\Documents\code\grunt\grunt.js
Line: 2
Char: 1
Error: 'module' is undefined
Code: 800A1391
Source: Microsoft JScript runtime error
As explained in the FAQ, you need to type grunt.cmd instead on Windows because the OS tries to launch grunt.js
Or you can install grunt-cli globally instead. This package will run any version of Grunt if it's been installed locally to your project.
SOLVED !!
So, this problem occur because windows by default associative < *.js > files
with >>
"Microsoft Windows Based Script Host".
grunt need to open by default with (grunt.cmd).
it easy to slove, by change default app (open with..)
Guide :
Go to any javascript file with "js" extension. (any file)
Right-Click(mouse) > Properties > "Opens with:" Change...(button)
Choose Notepad ( or any javascript IDE ).
PROBLEM SOLVED ! :)
good luck
If you're getting a "Microsoft JScript runtime error" that means that node.js isn't even getting invoked; instead Windows Script Host is trying to run your code. That's probably a problem with filetype associations; IIRC Windows defaults to trying to execute a ".js" file with WSH. You may wind up having to create a shortcut to your script, specifying a command line (probably something like "node %1") and a starting directory in order to make sure that it's executed properly.
It would help if you could tell us exactly how you're trying to invoke your code.
it seems that in the latest versions of the grunt modules, you would have to do the following to have it work under windows:
remove any globally installed grunt
npm uninstall -g grunt
install grunt-cli globally
npm install -g grunt-cli
install grunt locally into your project
npm install grunt
installing grunt (v0.4.x) globally does not seem to create the necessary grunt.cmd anymore. it seems that the recommendation is now to have grunt installed locally to be able to use version-specific Gruntfiles
As Florian F suggested, running grunt.cmd works. This is because of the process Windows is looking for your grunt command.
When typing grunt -h Windows will proceed to look for the following files:
./grunt.cmd
./grunt.* (grunt.js is found in this case which is why you see "module is undefined")
%APPDATA%/npm/grunt.cmd
An alternative to using "grunt.cmd" is to use grunter which simply renames the command to grunter... then you no longer have this problem.
To answer this, first we need to understand that the error is caused because it is being executed by Windows Script Host.
Now, run the code from your cmd promt with the following syntax:
>node <application_name>.js
this will allow the Node.js application to open through V8 JavaScript engine(Google's).
P.S: Please reply back if this has helped in resolving your issue else post the problem you are facing after trying this.
I had a similar issue, the problem is file association, I would recommend:
right click on a .js file and choose open with.
then you choose nodejs/node.exe (somewhere in "program files" folder
then make tick box where it says "always open .js files " (paraphrasing)
That should do the trick.
I went through the same issue when running an old Node project.
The issue was with the name of the js file, it was node.js. So the while running the command node node.js, it was opening up a windows dialogue box.
I just changed the name of the file to app.js and the error flew away.
So, in my case i had tryed all the mentioned above with no result.
But i have fund that im dont type: node in the full sentence as the following snipet
node script.js.And remember never understimate your own miscoding.
Solution:
Go to any javascript file with "js" extension. (any file)
Right-Click(mouse) > Properties > "Opens with:" Change...(button)
Choose Notepad ( or any Javascript IDE like VS Code ).

Resources