Category Archives: JavaScript

[Go] Can structure/structure pointer be compared with operator == is not defined error

Can two instances of the same struct be compared == !=
Answer: yes or no

Can two different instances of struct be compared == !=
Answer: yes or no

If all the member variables of the structure are comparable, then the structure can be compared.
If there are incomparable member variables in the structure, then the structure cannot be compared

    type s2 struct {
        name string
    }
    aa: = s2{
        name: " aa " ,
    }
    bb: = s2{
        name: " aa " ,
    }
    fmt.Printf( " %v\n " , aa == bb)

This returns true

If it is a structure pointer, return false

 

When there are incomparable fields, an error will be reported at compile time

 

 Change to a structure pointer, there will be no error

 

 Return result false; true

Code:

    type s1 struct {
        one map[ string ] string 
        two [] string 
        three string
    }

    a: = & s1{
        one: map[ string ] string { " aaa " : " bbb " },
        two: [] string { " aaa " , " bbb " },
        three: " aaaa " ,
    }
    b: = & s1{
        one: map[ string ] string { " aaa " : " bbb " },
        two: [] string { " aaa " , " bbb " },
        three: " aaaa " ,
    }
    c : = a
    fmt.Printf( " %v;%v " , a == b, a == c)

[Go]Understand the golang project performance analysis tool PProf

PProf uses profile.proto to analyze data

Can collect command analysis when the program is executed

Run-time data analysis during HTTP service can be collected

Can be analyzed by go test test case

 

There are the following monitoring and analysis functions:

CPU analysis, memory analysis, blocking analysis, mutex analysis, Groutine analysis

 

If it is an http service, then it can be achieved by directly importing this package _ “net/http/pprof”

If you use the gin framework, you need to import this package github.com/gin-contrib/pprof

And register the gin object pprof.Register(g)

 

Directly visit http://service/debug/pprof/ through the browser

You can see the following page

 

 

 The meaning of each parameter is:

allocs: view all past memory allocation sample
blocks: view the stack trace that caused blocking synchronization
cmdline: the complete call path of the command line of the current program
goroutine: view all currently running goroutines stack trace
heap: view the memory allocation of active objects
mutex: View the stack trace
profile of the competing holder that caused the mutex lock : CPU Profiling is performed for 30s by default, and a profile file for analysis is obtained.
threadcreate: View the stack trace of creating a new OS thread

 

Use the interactive command line to analyze the results of the above URL

For example, analyze the resident memory situation

go tool pprof -inuse_space http://localhost:8081/debug/pprof/heap input top command

 

 

 

For example, analyze the temporary allocation of memory

go tool pprof -alloc_objects http://localhost:8081/debug/pprof/heap input top command

 

 

 

Analyze goroutine

 go tool pprof http://localhost:6060/debug/pprof/goroutine You can use traces to see the call stack

The focus is on the independent goroutine that I opened myself, and the bottom one is the call function of the package I wrote.

[Go]Understand the golang project performance analysis tool trace

When using PProf is not too detailed, you can use trace to view the trace.

This command can be used with PProf

Download the trace file first

curl http://domain name/debug/pprof/trace?seconds=20> trace.out 

 

use

go tool trace C:\Users\shihan1\Downloads\trace.out

Because it is monitoring 127.0.0.1, it may be inconvenient to access on the server line

 

 

 

To use this tool, you need to install graphviz first

Windows system can go here to download, pay attention to check and add environment variables during installation, otherwise you need to manually add

http://www.graphviz.org/download/#windows

You can see the analysis by visiting the address

 

 

Javascript: js websocket disconnected reconnect library ReconnectingWebSocket

When websocket is connected, it is affected by the network

Or if the communication is not closed by the server for a long time, the disconnection reconnection mechanism is required

 

It is more troublesome to write the disconnection and reconnection by yourself, you can use this js library ReconnectingWebSocket.js

 https://github.com/joewalnes/reconnecting-websocket/ Download the min file directly, just import it

When using, only need to replace h5’s native websocket with ReconnectingWebSocket, everything else remains the same

For example: this is the use of this in vue.socket is the global ReconnectingWebSocket object, and other callback functions are also defined on the vue method

            the this .socket = new new ReconnectingWebSocket ( " xxxxxx " ); // Create Socket instance 
            the this .socket.debug = to true ;
             the this .socket.timeoutInterval = 10000 ; // connection timeout 
            the this .socket.reconnectInterval = 5000 ; // reconnection Interval time 
            this .socket.maxReconnectInterval = 600000 ; // Maximum reconnect interval time 
            this .socket.maxReconnectAttempts = 10 ; // Maximum number of reconnect attempts 
            this .socket.onmessage = this.OnMessage;
             this .socket.onopen = this .OnOpen;
             this .socket.onerror = this .OnError;
             this .socket.onclose = this .OnClose;

 

 If there is no communication for more than one minute, it will be interrupted, and then automatically reconnect

Javascript: Simple package localStorge operation

Determine whether the browser supports localStorge

Determine whether the browser is in incognito mode

Simple json encoding

 

// Storage localStorge 
function setLocalStorage(key,obj){
     if (!navigator.cookieEnabled|| typeof window.localStorage == ' undefined ' ){
         return  false ;
    }
    localStorage.setItem(key, JSON.stringify(obj));
    return  true ;
}
// Read localStorge 
function getLocalStorage(key){
     if (!navigator.cookieEnabled|| typeof window.localStorage == ' undefined ' ){
         return  false ;
    }
    var str = localStorage.getItem(key);
     if (! str){
         return  false ;
    }
    return JSON.parse(str);
}

NPM run dev runs the Vue project and reports an error: Node Sass does not yet support your current environment

After importing the Vue project, #npm run dev reported an error:

1
2
error in ./src/pages/hello.vue
Module build failed: Error: Node Sass does not yet support your current environment.......

 

 

Roughly it is a failure to compile a certain vue file, because the module fails to be constructed, and node sass does not support the current environment [mainly node].

Let’s take a look at the node version first:

    #node –version

 

Upgrading the node version/node version is too high will cause node-sass incompatibility, then just install node-sass again. 

1
#npm i node-sass -D

 [<==>#npm install node-sass -save-dev]

VueUncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: “/

Errors caused by repeated triggering of vue router routing

VueUncaught ( in promise) NavigationDuplicated: Avoided redundant navigation to current location: " /

 or

vue Uncaught Error: Redirected when going from “/*“ to “/*“
Don't talk nonsense

method 1:

This solution can be possible, but method 2 feels better

 this .$router.replace({
        path: this .$route.path,
        query
   })
 . catch (()=>()); // throw error

Method 2:

It is recommended to take a closer look at the discussion plan on github that is very detailed   https://github.com/vuejs/vue-router/issues/2881

// Initialize the injection routing place 
import Vue from'vue' 
import Router from'vue-router'

const originalPush = Router.prototype.push;
Router.prototype.push = function push(location, onResolve, onReject) {
     if (onResolve || onReject) return originalPush.call( this , location, onResolve, onReject);
     return originalPush.call( this , location). catch (err => err);
};

Vue.use(Router)

...
const router = new Router({
  routes
})

...

Solution to some map files in JS folder after Vue packaging (remove the map. JS file)

Solution to some map files in JS folder after Vue packaging (remove map.js file)

1. Enter the directory under the project: project package/config/index. JS
find productionsourcemap

 /*
   Source Maps
   */
    productionSourceMap: true,  // Change true to false on this side
    // https://webpack.js.org/configuration/devtool/#production
    devtool: '#source-map',

Productionsourcemap: true,// change the true here to false

How to Solve Node start error listen eacces 0.0.0.0:810

Error Message:
events. js:141
throw er; // Unhandled ‘error’ event
^
Error: listen EACCES 0.0.0.0:80
at Object.exports. _errnoException (util.js:870:11)
at exports. _exceptionWithHostPort (util.js:893:20)
at Server. _listen2 (net.js:1224:19)
at listen (net.js:1273:10)
at net. js:1382:9
at nextTickCallbackWith3Args (node.js:452:9)
at process. _tickCallback (node.js:358:17)
at Function.Module.runMain (module.js:444:11)
at startup (node.js:136:18)
at node.js:966:3
———————————-

1. one is the port is occupied
2. sudo node xx. js — root privileges are required to access ports below 1024