One Golang project Note (i)

Source: Internet
Author: User

Recently want to seriously study Golang, the best way to learn is certainly to do a project slightly. The Project Server is written with go, and the front end is intended to be done with Vue. The function of the project is that go can periodically get some network data through the crawler and then display the data via HTTP interface vue.

In the beginning, the problem to be solved was to communicate with each sub-process in a way that shared memory, because it was the quickest. Because C + + has previously been useful for createfilemapping and openfilemapping, it has also researched whether go has a related library. Found a syscall library has some system calls related functions, the Syscall library is described as follows:

Source: https://blog.csdn.net/erlib/article/details/50264341

syscall Encapsulation of Go language library We know that go is a native programming language for system-level development, and C/c++ is similar, the Go compiler compiles the program directly, linking the cost to the executable file. In theory, it can accomplish any C + +the language can be done. As an important aspect of supporting this feature, go provides a syscall package in the form of a standard library to support OS-level system calls. First, go encapsulates various system invocation interfaces, providing the user with a set of go language functions that are easy to invoke directly in the program, such as Func Read (FDint, p []byte) (nint, err Error) func Write (FDint, p []byte) (nint, err Error) at the same time, go also provides direct call support for Syscall through the following functions: Func Syscall (Trap, a1, A2, A3 uintptr) (R1, R2 uintptr, err Errno) func SYSC ALL6 (Trap, a1, A2, A3, A4, A5, A6 uintptr) (R1, R2 uintptr, err Errno) func Rawsyscall (Trap, a1, A2, A3 uintptr) (R1, R2 UI Ntptr, err Errno) func RawSyscall6 (Trap, a1, A2, A3, A4, A5, A6 uintptr) (R1, R2 uintptr, err Errno) where a set of operations with a raw prefix represents a direct call to Sy Scall (note: In Linux, for example, in the AMD64 is implemented by the Syscall directive, in the X86 is an int 0x80 soft interrupt, and arm is the use of SWI soft interrupt implementation system calls), An operation without a raw prefix calls runtime Entersyscall before actually calling Syscall, and inserts runtime Exitsyscall after Syscall returns. The functions of these two auxiliary functions we have already said in the previous introduction of the scheduler, will be mentioned later. These 4 functions are all implemented in assembly language, and related to the specific hardware architecture and OS, such as the corresponding implementation of the Linux under the ARM architecture, in the SRCThe/PKG/SYSCALL/ASM_LINUX_ARM.S. As for other functions such as read/write, the interior of the function is basically called the above 4 functions.

Sure enough to find syscall below have createfilemapping function, but there is no openfilemapping, but after searching a data on the Internet, only to find someone to use to load Kernel32.dll System DLL Library way, Calling one of the openfilemapping methods, I think this method is slightly cumbersome and may later migrate Linux bad, so give up this way. Later found to use the CreateFileMapping function as can, only need to read the time to use the read-only mode, then use it first.

Use the CreateFileMapping sample code as follows:

Service side (write memory)

Import( "bytes" "FMT" "Log" "Syscall" "Time" "Unicode/utf16" "Unicode/utf8" "unsafe")
func Main() { file, _: = Syscall. utf16ptrfromstring( "Sharememory") size:= 100000 //I ' ve tried unsafe. Sizeof (mumbledata{}) but that's didn ' t work. Handle, Err: = Syscall. createfilemapping( 0, Nil, Syscall. Page_readwrite, 0, UInt32(size), file) ifErr! = Nil{log. Fatal(ERR)} deferSyscall. CloseHandle(handle) Addr, Err: = Syscall. MapViewOfFile(Handle, Syscall. File_map_write, 0, 0, 0) ifErr! = Nil{log. Fatal(ERR)} var I byte= 0x30 for{ Data: = (*test) (unsafe. Pointer(addr)) data.str[ 0] = i i++ data.str[ 1] = i time. Sleep( 1* Time. Second) //FMT. Printf ("Ava%v cam%v ID%v\n", data. Avatar.position, data. Camera, data. Identity)Fmt. Printf( "Str:%s \ n ", string(data.str[:])) i++}}

Client (read memory)

func Main() { file, _: = Syscall. utf16ptrfromstring( "Sharememory") size:= 100000 //I ' ve tried unsafe. Sizeof (mumbledata{}) but that's didn ' t work. Handle, Err: = Syscall. createfilemapping( 0, Nil, Syscall. Page_readonly, 0, UInt32(size), file) ifErr! = Nil{log. Fatal(ERR)} deferSyscall. CloseHandle(handle) FMT. Println(Syscall. GetLastError())
Addr, Err: = Syscall. MapViewOfFile(Handle, Syscall. File_map_read, 0, 0, 0) ifErr! = Nil{log. Fatal(ERR)} deferSyscall. UnmapViewOfFile(addr) for{ Data: = (*test) (unsafe. Pointer(addr))
Time. Sleep( 1* Time. Second) //FMT. Printf ("Ava%v cam%v ID%v\n", data. Avatar.position, data. Camera, data. Identity)Fmt. Printf( "Str:%s \ n ", string(data.str[:])) }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.